Search code examples
iphoneavplayeravaudiosessioninterruption

How to get interruption start/end events for AVPlayer


I am playing audio files with AVPlayer. I 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 fine when an interruption came while the app is in foreground(like voice control).
But I made the app in background mode and opened the iPod player and start playing. Its interruption is not getting called at that time and even when my app entered foreground.
What may be the problem. Please help.


Solution

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

    More methods listed in the docs.