How to resume background music after an outgoing phone call (iOS)?

I am using AVAudioPlayer to play music (background support). My question is: if I want to call someone while I listen to music, how can I resume it after a conversation? I implement the AVAudioPlayer delegation method:

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)thePlayer { [[AVAudioSession sharedInstance] setActive:YES error:nil]; [self.player play]; } 

but this will not allow the music to continue.

I also tried using the AVAudioSessionDelegate method (just try):

 - (void)viewDidLoad { [[AVAudioSession sharedInstance] setDelegate:self]; } - (void)endInterruption { [[AVAudioSession sharedInstance] setActive:YES error:nil]; [self.player play]; } 

but again, this will not lead to the resumption of music. Any ideas on how to solve this problem?

+7
source share
3 answers

strange from your code, you say you use AVAudioPlayer, why do you then implement AVAudioSessionDelegate?

And you have a poor implementation of delegate methods. See Documents

from AVAudioPlayerDelegate:

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags

So try to implement them like this and it should work correctly

+1
source

In iOS, the application cannot start playing sound when it is already in the background. If your application is disconnected because the foreground application has taken control of the audio session, there is currently no way to get it back.

+1
source

You can renew it. At the end of the audio session, you must notify another application that my application disconnects the audio session, and you can use it. Use the following code.

 [[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]; 
+1
source

All Articles