我知道AVPlayerItem(AVPlayer的子类)能够订阅AVPlayerItemDidPlayToEndTimeNotification
AVPlayer没有这样的功能,我需要使用AVPlayer,除非没有办法解决这个问题,并且知道(我的情况下是视频)的播放时间是否完整。
如何在AVPlayer中执行此操作?我阅读了这些课程的一些文档,但我没有立即得到答案。
-(void)setupVideoView{
_videoPlayer = [AVPlayer playerWithURL:urlObjectHere];
AVPlayerLayer *layer = [AVPlayerLayer layer];
[layer setPlayer:_videoPlayer];
[layer setFrame:_videoView.bounds];
[_videoView.layer addSublayer:layer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:_videoPlayer];
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// does not get called
DLog(@"");
}
答案 0 :(得分:1)
据我所知,AVPlayer层无法知道完整的时间。
如果您只需要AVPlayer的普通实用程序,我认为只需在AVKit中使用AVPlayerViewController。
建议不要使用MPMoviePlayer,大部分都已弃用。习惯了AVPlayer ......
答案 1 :(得分:1)
我能够通过以下方式解决问题:我使用AVPlayerItem并从该项目创建了我的AVPlayer,将通知订阅到播放器项目。
-(void)setupVideoView{
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:urlObjectHere];//edited
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];//edited
_videoPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];//edited
AVPlayerLayer *layer = [AVPlayerLayer layer];
[layer setPlayer:_videoPlayer];
[layer setFrame:_videoView.bounds];
[_videoView.layer addSublayer:layer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:_videoPlayer];
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// gets called finally
AVPlayerItem *p = [notification object]; //bring video back to zero
[p seekToTime:kCMTimeZero];
DLog(@"");
}