5

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.

Tinku George
  • 195
  • 2
  • 11

2 Answers2

4

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.

Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
  • Thanks for your response. I got the delegate of AVAudioPlayer. But I would like to know whether there is any way to implement this using AVPlayer. Can you suggest an idea. – Tinku George Mar 19 '13 at 15:41
  • I don't have a lot of experience with AVPlayer but this SO question covers the same issue: http://stackoverflow.com/questions/6837002/no-avplayer-delegate-how-to-track-when-song-finished-playing-objective-c-iphon – Ryan Poolos Mar 19 '13 at 15:54
  • Anyway I am changing my AVPlayer to AVAudioPlayer. Thanks for your advice. – Tinku George Mar 19 '13 at 18:16
  • Haha well glad I could help. Happy Coding. :) – Ryan Poolos Mar 19 '13 at 18:19
  • 1
    This does not work if you are trying to stream audio from the web. – Guy Lowe May 01 '15 at 05:43
1

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];
KIDdAe
  • 2,714
  • 2
  • 22
  • 29