我在我的应用程序中使用AVPlayerLayer
和构建了一个视频播放器AVPlayer
。
选择新视频时,我将做出此方法:
//this to remove the current video
if (avPlayerLayer) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[audioPlayer currentItem]];
[avPlayerLayer.player pause];
[avPlayerLayer removeFromSuperlayer];
avPlayerLayer = nil;
}
//and this is to add a new one
audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:fileName]];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:audioPlayer] retain];
[avPlayerLayer setFrame:self.view.bounds];
CGRect frame = avPlayerLayer.frame;
[avPlayerLayer setFrame:CGRectMake(frame.origin.x, frame.origin.y - 30, frame.size.width, frame.size.height)];
[[self.view layer] addSublayer:avPlayerLayer];
[audioPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(finishPlayingSong)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[audioPlayer currentItem]];
[audioPlayer release];
现在,有时当我执行此方法时,设备不会开始播放视频(它是同时发生的,而不是在同一个视频上)。知道为什么会这样吗?我该如何处理?
编辑
我注意到它发生在我播放 5 首歌曲之后。