播放視頻以前我們可以使用MPMoviePlayerController,雖然MP很簡單,但是不能定制UI,并且很多功能不能實現,AVFoundation中的AVPlayer應運而生,首先我們來看一幅圖:
AVPlayer.png
實現一個簡單的網絡視頻播放器,需要注意三個重要對象:
(1)AVPlayer:負責播放視頻
(2)AVPlayerItem:負責管理視頻數據,apple給出的API解釋:A player item manages the presentation state of an asset with which it is associated. A player item contains player item tracks—instances of
[AVPlayerItemTrack](https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVPlayerItemTrack_Class/Reference/Reference.html#//apple_ref/occ/cl/AVPlayerItemTrack)
—that correspond to the tracks in the asset(3) AVPlayerLayer:視頻播放界面顯示圖層
廢話不多說,直接上代碼:
static void *PlayViewStatusObservationContext = &PlayViewStatusObservationContext;
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_player pause];
_player = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]];
//播放狀態
[item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
//緩沖總時間
[item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
// 緩沖區空了,需要等待數據
[item addObserver:self forKeyPath:@"playbackBufferEmpty" options: NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
// 緩沖區有足夠數據可以播放了
[item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options: NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
_player = [[AVPlayer alloc] initWithPlayerItem:item];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
layer.frame = self.view.bounds;
layer.contentsScale = [UIScreen mainScreen].scale;
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:layer];
_currentTime = [[UILabel alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 20, 120, 20)];
_currentTime.textAlignment = 1;
_currentTime.textColor = [UIColor whiteColor];
_currentTime.backgroundColor = [UIColor blackColor];
[self.view addSubview:_currentTime];
_slider = [[UISlider alloc] initWithFrame:CGRectMake(120, [UIScreen mainScreen].bounds.size.height - 20, [UIScreen mainScreen].bounds.size.width - 240, 5.0)];
[self.view addSubview:_slider];
_totalTime = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 120, _slider.frame.origin.y, 120, 20)];
_totalTime.textAlignment = 1;
_totalTime.textColor = [UIColor whiteColor];
_totalTime.backgroundColor = [UIColor blackColor];
[self.view addSubview:_totalTime];
_totalTime.text = [self toTimeStrWithSeconds:CMTimeGetSeconds(_player.currentItem.duration)];
[_slider addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventValueChanged];
//播放器添加定時器,CMTimtMake(a,b)相當于a/b秒就會進入這個block
[_player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
queue:dispatch_get_main_queue()
usingBlock:^(CMTime time) {
float currentValue = CMTimeGetSeconds(time);
float totalValue = CMTimeGetSeconds(item.duration);
_totalTime.text = [self toTimeStrWithSeconds:totalValue];
_currentTime.text = [self toTimeStrWithSeconds:currentValue];
[_slider setValue: currentValue / totalValue];
}];
//播放進度相關
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playtoEndAction:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(jumpedTimeAction:) name:AVPlayerItemTimeJumpedNotification object:_player];
//播放后臺相關
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBackground:)
name:UIApplicationDidEnterBackgroundNotification object:_player];
注意,在這里添加KVO操作的是AVPayerItem對象而不是AVPlayer對象,注意在dealloc里面移除KVO,在KVO方法里面可以獲取Item的一些數據,比如播放狀態、緩沖數據大小等等,這對于我們定制播放界面UI很有幫助,像開發者可以自己定制進度條,獲取播放進度、獲取緩沖進度等等:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([object isKindOfClass:[AVPlayerItem class]]) {
AVPlayerItem *item =(AVPlayerItem *)object;
if ([keyPath rangeOfString:@"status"].length) {
//播放狀態
if (item.status == AVPlayerStatusReadyToPlay) {
[_player play];
} else if (item.status == AVPlayerStatusFailed) {
NSLog(@"失敗!");
} else if (item.status == AVPlayerStatusUnknown) {
NSLog(@"未知!");
}
} else if ([keyPath rangeOfString:@"loadedTimeRanges"].length) {
//緩沖
NSArray *caches = item.loadedTimeRanges;
CMTimeRange range = [caches.firstObject CMTimeRangeValue];
float startSeconds = CMTimeGetSeconds(range.start);
float durationSeconds = CMTimeGetSeconds(range.duration);
float cachesSeconds = startSeconds + durationSeconds;
NSString *subStr = @"%";
float totalDuration = CMTimeGetSeconds(item.duration);
NSLog(@"共緩沖了%@%.2f",subStr,cachesSeconds / totalDuration * 100.0);
}
}
}