需要导入两个系统库文件CoreMedia.framework 和 AVFoundation.framework,
MediaPlayer-Prefix.pch 文件
#import
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import
#import
//在预编译头文件中导入头文件A 相当于在每个.m中导入该头文件A 因为基本上每个.m文件中都要用到
#import
#endif
注意:将 ViewController.xib 要关联的类设置为WJPlayView,操作在之前的博客中说过。
ViewController.h 文件
#import
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
- (IBAction)playBtnClicked:(id)sender;
- (IBAction)sliderProgerssValue:(id)sender;
@end
#import "ViewController.h"
@interface ViewController ()
{
//实例化一个电影对象
AVPlayerItem *myMovie;
//实例化一个播放器
AVPlayer *myPlay;
NSTimer * refreshTimer;
}
- (void)refreshProgress;
@end
@implementation ViewController
//weatherClicked开始为NO,表示播放。当第一次点击按钮时,变为YES,表示暂停
BOOL weatherClicked = NO;
- (void)viewDidLoad
{
[super viewDidLoad];
//全屏播放
self.wantsFullScreenLayout = YES;
//屏幕大小,不写死的写法
self.view.frame = [UIScreen mainScreen].bounds;
self.view.backgroundColor = [UIColor blackColor];
//播放网络视频
//myMovie = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://ocj2.smgbb.cn/ocj2/ocj2.m3u8"]];
//播放本地音频,视频文件
NSString *movePath = [[NSBundle mainBundle] pathForResource:@"bsm" ofType:@"mp4"];
myMovie = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:movePath]];
myPlay = [AVPlayer playerWithPlayerItem:myMovie];
//
AVPlayerLayer *layer = (AVPlayerLayer *)self.view.layer;
layer.player = myPlay;
[myPlay play];
//记住网络视屏的时间不能用下面的方法获取,只能在 http 请求头中去获取,下面的方法只能用于读取本地文件
CMTime totalTime = myMovie.duration;
int seconds = totalTime.value / totalTime.timescale;
_slider.maximumValue = seconds;
_slider.minimumValue = 0;
_totalTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",seconds / 3600,(seconds % 3600) / 60, (seconds % 3600) % 60];
_currentTimeLabel.text = @"00:00:00";
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1/24.0f target:self selector:@selector(refreshProgress) userInfo:nil repeats:YES];
}
- (void)refreshProgress
{
CMTime currentTime = myMovie.currentTime;
int curPlayTimeSeconds = currentTime.value / currentTime.timescale;
_slider.value = curPlayTimeSeconds;
_currentTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",curPlayTimeSeconds / 3600,(curPlayTimeSeconds % 3600) / 60, (curPlayTimeSeconds % 3600) % 60];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playBtnClicked:(id)sender {
//每次点击后要取反
weatherClicked = !weatherClicked;
if (weatherClicked) {
[myPlay pause];
//暂停定时器
[refreshTimer isValid];
//注意:下面这句是销毁定时器
//[refreshTimer invalidate];
}else{
[myPlay play];
//重新开启定时器
[refreshTimer fire];
}
}
- (IBAction)sliderProgerssValue:(id)sender {
CMTime curTimer = myMovie.currentTime;
curTimer.value = _slider.value * curTimer.timescale;
[myPlay seekToTime:curTimer];
int seconds = (int)_slider.value;
_currentTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",seconds / 3600,(seconds % 3600) / 60, (seconds % 3600) % 60];
}
//点击隐藏状态栏和下面的进度条,按钮等,再点击又显示出来
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([UIApplication sharedApplication].statusBarHidden) {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
[UIView animateWithDuration:0.5f animations:^{
[_backView setAlpha:1];
}];
}else{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
[UIView animateWithDuration:0.5f animations:^{
[_backView setAlpha:0];
}];
}
}
@end
#import
@interface WJPlayView : UIView
@end
#import "WJPlayView.h"
@implementation WJPlayView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//获取view的layer的时候返回avlayer
+(Class)layerClass{
return [AVPlayerLayer class];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end