How to get start and end interrupt events for AVPlayer

I play audio files from AVPlayer. I have implemented AVAudioSessionInterruptionNotification.

    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *errorInAudio   = nil;
    [session setActive:YES error:&errorInAudio];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil];

    [session setCategory:AVAudioSessionCategoryPlayback error:nil];

It works great when an interrupt arrives when the application is in the foreground (e.g. voice control).
But I made the application in the background and opened the iPod and started playing. Its interruption is not called at that time and even when my application has come to the fore.
What could be the problem. Please, help.

+5
source share
2 answers

If you use AVAudioPlayer, you can match AVAudioPlayerDelegate, then you can implement methods like- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

, docs.

+3

, AVPlayer , AudioSession

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStopped) name:AVAudioSessionInterruptionNotification object:nil];
+1

All Articles