AVPlayerItemDidPlayToEndTimeNotification not called

亡梦爱人 提交于 2020-01-15 12:57:50

问题


I have a video on a screen that must be played with an infinity loop.

So I wrote :

self.player = [AVPlayer playerWithPlayerItem:avItem];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[_player currentItem]];

[_player play];

And I fallback method is :

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    [_player.currentItem seekToTime:kCMTimeZero];
    [_player play];
}

It works well but sometimes it seems that my fallback is not called. The video is freezing at the end and never played again. It's happening randomly ...

Do you have an idea ?


回答1:


You can just set a boundary time observer.

    - (id) setupBoundaryEndWith:(NSArray*)array
    {
        __weak MY_AVPlayer* weakself = self;
        return [self addBoundaryTimeObserverForTimes:array queue:NULL usingBlock:^{
            [weakself stopAVPlayerAndLoopOrTriggerNextTrack];
            }];
    }



回答2:


[_player currentItem] is null, since you haven't started playing yet. I would suggest either to explicitly add the AVPlayerItem (ideally) or register to notifications after starting the playback (reverse the 2 last lines).

I have not tried the 2-nd solution, so it might not work, if it takes some time to start the playback. If that is the case, I suggest setting an NSTimer to trigger a second later and then register.



来源:https://stackoverflow.com/questions/23904436/avplayeritemdidplaytoendtimenotification-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!