第一次看到简书,感觉就喜欢上了它,想我这么懒的人,看到简书后,第一次有了写点什么东西的欲望。工作也有一段时间了,有时候很想把工作上学到的东西或者一些感悟写下来,但总是由于种种原因没有动手。以后工作的时间,每天抽半个小时时间,写技术博客,自认为能力还远远不够,记录下每日点滴感悟,便是足够了,希望能够坚持下去。
做视频播放,边下边播,都做了一个多月了,前前后后3套方案,最后终于算是完美的解决了。今天关于avplayer又学到了一点新的知识。
1.一般情况下需要实现的4个监听属性
//预播放状态,有三种情况AVPlayerItemStatusUnknown,AVPlayerItemStatusReadyToPlay,AVPlayerItemStatusFailed
[self.videoPlayItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//缓冲进度,可有可无,可以增加用户体验
[self.videoPlayItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
//seekToTime后,缓冲数据为空,而且有效时间内数据无法补充,播放失败
[self.videoPlayItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
//seekToTime后,可以正常播放,相当于readyToPlay,一般拖动滑竿菊花转,到了这个这个状态菊花隐藏
[self.videoPlayItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
2. 一般情况下需要监听的4个通知中心
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(moviePlayDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_videoPlayItem]; // 添加视频播放结束通知
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(moviePlayInterrupt:)
name:AVPlayerItemPlaybackStalledNotification
object:_videoPlayItem]; //添加视频异常中断通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(enterBcakground:)
name:UIApplicationWillResignActiveNotification
object:nil]; //进入后台
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(enterPlayGround:)
name:UIApplicationDidBecomeActiveNotification
object:nil]; // 返回前台
3. rate属性
avplayer自身有一个rate属性
rate ==1.0,表示正在播放;rate == 0.0,暂停;rate == -1.0,播放失败
为了严谨,可以这样判断播放器状态
if (avplayer.rate > 0 && avplayer.error == nil) {
//playing
} else {
//failed
}
4. slider
为了更好地用户体验,一般在滑竿滑动过程中,停止播放时间的走动,当前时间的显示随滑竿移动而改变,当手指离开滑竿后,播放器从当前点开始播放,在从当前点开始播放的时候,有可能没有缓冲数据,需要临时加载,这时候就需要掏用到上面的方法来判断当前状态,加载数据还没有播放的时候,时间走动需要停止,当开始播放了,才开始时间走动。
[_playSlider addTarget:self action:@selector(playSliderChange:) forControlEvents:UIControlEventValueChanged]; //拖动滑竿更新时间
[_playSlider addTarget:self action:@selector(playSliderChangeEnd:) forControlEvents:UIControlEventTouchUpInside]; //松手,滑块拖动停止
//在松手的时候,也有可能会走下面其中某一个时间,为了严谨,建议加上这些事件,当然,松手的时候,只可能走这3个事件中的某一个
[_playSlider addTarget:self action:@selector(playSliderChangeOutEnd:) forControlEvents:UIControlEventTouchUpOutside];
[_playSlider addTarget:self action:@selector(playSliderChangeCancel:) forControlEvents:UIControlEventTouchCancel];
5.视频播放时间显示小技巧
这样写,不管时间有多长,都可以按照 小时:分钟:秒数 的形式来显示,简单吧!
NSString *str = nil;
if (videocurrent < 3600) {
str = [NSString stringWithFormat:@"%02li:%02li",lround(floor(videocurrent/60.f)),lround(floor(videocurrent/1.f))%60];
} else {
str = [NSString stringWithFormat:@"%02li:%02li:%02li",lround(floor(videocurrent/3600.f)),lround(floor(videocurrent%3600)/60.f),lround(floor(videocurrent/1.f))%60];
}
6.附上比较重要的一段代码,监听事件
//kvo监听播放状态
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
AVPlayerItem *playerItem = (AVPlayerItem *)object;
if ([keyPath isEqualToString:@"status"]) {
if ([playerItem status] == AVPlayerStatusReadyToPlay) {
CGFloat totalSecond = playerItem.duration.value / playerItem.duration.timescale;// 转换成秒
[self updateTotolTime:totalSecond];
[self setPlaySliderValue:totalSecond];
_videoLength = totalSecond;
[self monitoringPlayback:_videoPlayItem];// 监听播放状态
NSLog(@"readytoPlayreadytoPlayreadytoPlayreadytoPlay");
if ([self.delegate respondsToSelector:@selector(TBAVplayerReadyToPlay:)]) {
[self.delegate TBAVplayerReadyToPlay:self];
}
[_showView.layer addSublayer:self.videoPlayerLayer];
} else if ([playerItem status] == AVPlayerStatusFailed || [playerItem status] == AVPlayerStatusUnknown) {
if ([self.delegate respondsToSelector:@selector(TBAVPlayerPlayReadyFailed:)]) {
[self.delegate TBAVPlayerPlayReadyFailed:self];
}
}
} else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
if (_isFinishLoading || _localVideo) {
[self.videoProgressView setProgress:1 animated:NO]; //如果视频下载完成,把缓冲进度置为满格
} else {
NSTimeInterval timeInterval = [self availableDuration];// 计算缓冲进度
CMTime duration = self.videoPlayItem.duration;
CGFloat totalDuration = CMTimeGetSeconds(duration);
[self.videoProgressView setProgress:timeInterval / totalDuration animated:YES];
}
} else if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"]) {
if (playerItem.playbackBufferEmpty) {
//Your code here
[_videoPlayerLayer.player pause];
[_hudHelper showHudOnView:_showView caption:nil image:nil acitivity:YES autoHideTime:0];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_videoPlayerLayer.player play];
//rate 是avplayer 是一个属性,rate 1.0表示正在播放,0.0暂停, -1播放器失效
if (self.rate <= 0) {
[[iToast makeText:@"播放异常"] show];
}
});
}
} else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (playerItem.playbackLikelyToKeepUp)
{
_playState = YES;
//Your code here拖动之后开始播放
[_hudHelper hideHud];
}
}
}
网友评论