我在我的應用程序中使用AVPlayerLayer
和AVPlayer
構建了一個視頻播放器。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首歌后。
爲什麼你使用通知,當委託方法的avplayer可用?嘗試設置audioplayer.delegate = nil,同時將audioplayer設置爲零,並在初始化新玩家時,將該代表設置爲self。 –
AVPlayer有委託方法?你確定? – MTA
對不起,這是我的混亂,它的AVAudioplayer有代表方法。 –