Pierre
Pierre

Reputation: 11633

AVPlayerItemDidPlayToEndTimeNotification not called

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 ?

Upvotes: 1

Views: 3644

Answers (2)

cooperz
cooperz

Reputation: 23

[_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.

Upvotes: 2

amergin
amergin

Reputation: 3176

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];
            }];
    }

Upvotes: 2

Related Questions