在iOS9中,MPMoviePlayer类已全部弃用,转而支持AVPlayer。我有一个使用MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的现有应用程序来确定如何记录视频播放器结束的事件。我如何对AVPlayer做同样的事情?
以下是结束原因键:
答案 0 :(得分:4)
AVKit中没有与MPMoviePlayerPlaybackDidFinishReasonUserInfoKey和MPMoviePlayerPlaybackDidFinishNotification等效的内容。要在AVKit中完成相同的功能,您必须单独收听三个通知,而不是一个通知,原因可能不同。
如果您想知道视频是否正在播放,您可以进行KVO:
[self.player addObserver:self forKeyPath:@"rate" options:0 context:nil];
然后添加此方法:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"rate"]) {
if ([self.player rate]) {
[self changeToPause]; // This changes the button to Pause
}
else {
[self changeToPlay]; // This changes the button to Play
}
}
}
答案 1 :(得分:1)
尝试viewDidLoad
:
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"yoururl"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease];
[player play]
和
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
}