2015-11-14 113 views
0

我知道AVPlayerItem(AVPlayer的一個子類)能夠訂閱AVPlayerItemDidPlayToEndTimeNotification註冊AVPlayer事件 - 怎麼知道AVPlayer播放完畢的電影

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

我是否應該取消它並使用MPMoviePlayer來代替? – stackOverFlew

回答

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(@""); 



} 
1

據我所知,沒有辦法知道AVPlayer圖層的完整時間。

AVAsset - AVPlayerItem - AVPlayer是AVFoundation中結合AVPlayer的三個組件。如果您想DIY AVPlayer的更多功能,您需要使用較低級別的AVPlayer,它們是AVAsset和AVPlayerItem。

如果您只需要AVPlayer的普通實用程序,只需在AVKit中使用AVPlayerViewController即可。

建議不要使用MPMoviePlayer,它的大部分已被棄用。適應AVPlayer ...

+0

我明白了,謝謝你。不知道MPMoviePlayer是否被棄用。我想使用AVPlayerItem。我如何使它在UIView中播放並對其進行播放/暫停? – stackOverFlew

+0

再次感謝,我能夠通過使用AVPlayerItem解決我的問題。 – stackOverFlew

+1

沒問題,我唯一知道的關於AVPlayer完成時間的方法是,你知道,通過NSNotificationCenter,這是正確的 關於播放和暫停,有兩種實例方法[yourplayer play];和[你的播放暫停];並且你可以判斷你的player.rate> 0是否知道它是否在播放,rate是你播放器的播放速度 – StevenUpForever