How to resume AVAudioPlayer after interrupted in background
Asked Answered
A

2

7

I am playing music using AVAudioPlayer in background. The problem is: if there is a incoming calling interrupts the player, it will never resume unless switch to foreground and do it manually.

The code is simple, to play it in background:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord  error: nil];
[[AVAudioSession sharedInstance]  setActive: YES error: nil];

url = [[NSURL alloc] initFileURLWithPath:...];

audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL];
audio_player.delegate = self;
bool ret = [audio_player play];

delegate to handle interruptions:

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
    //tried this, not working [[AVAudioSession sharedInstance]  setActive: NO error: nil]; 
    NSLog(@"-- interrupted --");
}


//----------- THIS PART NOT WORKING WHEN RUNNING IN BACKGROUND ----------
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    NSLog(@"resume!");
    //--- tried, not working: [[AVAudioSession sharedInstance] setCategory:             AVAudioSessionCategoryPlayAndRecord  error: nil];
    //--- tried, not working: [[AVAudioSession sharedInstance]  setActive: YES error: nil];
    //--- tried, not working: [audio_player prepareToPlay];
    [audio_player play];
}

Can any one help me?

Apeak answered 30/12, 2011 at 5:31 Comment(2)
Same pb, did you find a solution ?Lamasery
so far, no. But there is a solution. Sample apple store apps have done that.Apeak
T
8

Found the solution! I had the same problem, my app was resuming the audio nicely after an interruption, only if my app was open. When it was on the background it failed ro resume playing the audio after an interruption.

I fixed this by adding the following lines of code:

Add this line whenever your app start playing audio.[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

And on the endInterruption method, wait 1 or 2 seconds before resume playing the audio. This to allow the OS to stop using the audio channel.

- (void)endInterruptionWithFlags:(NSUInteger)flags {
    // Validate if there are flags available.
    if (flags) {
        // Validate if the audio session is active and immediately ready to be used.
        if (AVAudioSessionInterruptionFlags_ShouldResume) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{
                    // Resume playing the audio.
                });
        }
    }
}

You can also add this line when your app stops (not pause) playing audio. But is not required. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Tragus answered 15/3, 2012 at 21:47 Comment(2)
Should I change this line if (AVAudioSessionInterruptionFlags_ShouldResume) to if (flags == AVAudioSessionInterruptionFlags_ShouldResume)Feathers
The use of AVAudioSessionDelegate protocol is deprecated in iOS 6Markmarkdown
M
0

Try this

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer withFlags:(NSUInteger)flags{

    if (flags == AVAudioSessionFlags_ResumePlay) {
        [audioPlayer play];
    }    

Hope it helps.

Manatarms answered 2/1, 2012 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.