DZVideoPlayerViewController icon indicating copy to clipboard operation
DZVideoPlayerViewController copied to clipboard

Prevent AVPlayer Notification from being called from another video being played else where in the application

Open vassily-fr opened this issue 7 years ago • 0 comments

AVPlayer is used to play loop video in my app. So when one of this loops ends, the notification is called what ever item is currently playing. So I simply fix this by changing the register for remote notifications.

- (void)setupNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAVPlayerItemDidPlayToEndTime:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAVPlayerItemFailedToPlayToEndTime:)
                                                 name:AVPlayerItemFailedToPlayToEndTimeNotification object:_player.currentItem];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAVPlayerItemPlaybackStalled:)
                                                 name:AVPlayerItemPlaybackStalledNotification object:_player.currentItem];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationDidBecomeActive:)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];
}

#pragma mark - Notification Handlers

- (void)handleAVPlayerItemDidPlayToEndTime:(NSNotification *)notification {
    if (notification.object == _player.currentItem)
    {
        [self stop];
        [self onDidPlayToEndTime];
    }
}

- (void)handleAVPlayerItemFailedToPlayToEndTime:(NSNotification *)notification {
    if (notification.object == _player.currentItem)
    {
        [self stop];
        [self onFailedToPlayToEndTime];
    }
}

- (void)handleAVPlayerItemPlaybackStalled:(NSNotification *)notification {
    if (notification.object == _player.currentItem)
    {
        [self pause];
        [self.activityIndicatorView startAnimating];
        [self onPlaybackStalled];
    }
}

I can't pullrequest on this project. don't know why.

Thanks

vassily-fr avatar Nov 28 '18 18:11 vassily-fr