Question

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.

Was it helpful?

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.

OTHER TIPS

To get notified when AVPlayer is interrupted in background you can listen notification of the AudioSession

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStopped) name:AVAudioSessionInterruptionNotification object:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top