Tinku George
Tinku George

Reputation: 195

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.

Upvotes: 5

Views: 4944

Answers (2)

KIDdAe
KIDdAe

Reputation: 2722

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];

Upvotes: 1

Ryan Poolos
Ryan Poolos

Reputation: 18551

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.

Upvotes: 4

Related Questions