Skip to content

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

@vassily-fr

Description

@vassily-fr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions