Chilly Zhong
Chilly Zhong

Reputation: 16793

How to resume background music after out-going phone call (iOS)?

I'm using AVAudioPlayer to play music (background supported). My question is if I want to call somebody while I'm listening to the music, how to resume it after the call? I've implement the AVAudioPlayer's delegate method:

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

but this will not allow the music to continue.

I've also tried to use the AVAudioSessionDelegate method (just a try):

- (void)viewDidLoad {
    [[AVAudioSession sharedInstance] setDelegate:self];
}

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

but again this won't cause the music to resume. Any ideas about how to solve this problem?

Upvotes: 7

Views: 2992

Answers (3)

Mohan Kumar
Mohan Kumar

Reputation: 334

You can resume it. After finishing your audio session you have to notify other app that my app deactivate the audio session and you can make use of it. Use the following code.

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

Upvotes: 1

hotpaw2
hotpaw2

Reputation: 70733

On iOS, an app can not start playing audio when already in the background. If your app's audio is stopped because a foreground app has taken control of the audio session, there is (currently) no way to take it back.

Upvotes: 1

Marek Sebera
Marek Sebera

Reputation: 40671

strange from your code is, you're telling that you do use AVAudioPlayer, why do you then implement AVAudioSessionDelegate?

And you have bad implementation of delegate methods. See docs

from AVAudioPlayerDelegate:

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player

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

So try to implement them like this, and it should be working properly

Upvotes: 1

Related Questions