6

I have an issue with MPMoviePlayerController. I use an instance to play an m3u8 audio source:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) {
}

NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) { 
}
self.player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:url]];
player.view.hidden = YES;
player.shouldAutoplay = YES;
[player release];
[btnContainer addSubview: player.view];
player.useApplicationAudioSession = NO;

it is designed to play when the app goes to background, and everything is working OK.

The problem is when it is in the background and I get an incoming call. In that case, the stream pauses, but doesn't come back after call ends. In fact, the console says

2011-01-12 12:02:27.729 RAC1[1571:307] MP _playbackInterruptionDidEndNotification :: NSConcreteNotification 0x155890 {name = AVController_PlaybackInterruptionDidEndNotification; object = <AVController: 0x180d50>; userInfo = {
    "AVController_InterruptionStatusNotificationParameter" = "call.declined";
    "AVController_InterruptorNameNotificationParameter" = Phone;
}}, _state = 6
2011-01-12 12:02:27.730 RAC1[1571:307] MP _playbackInterruptionDidEndNotification :: resuming playback!

and the app does show the stream as MPMoviePlaybackStatePlaying, but the sound seems to stop. I have tried doing

[[AVAudioSession sharedInstance] setActive: YES error: &err]

but it seems to fail.

Does anybody have a clue?

thanks!

Antonio
  • 863
  • 1
  • 11
  • 23

2 Answers2

3

It seems like I needed to

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

in viewDidAppear of my viewController....

Antonio
  • 863
  • 1
  • 11
  • 23
  • I begin receiving remote control events when the play button is pressed and end receiving control events when the stop button is pressed. Also I needed to wait 1 second before resume playing when the app is playing audio on background. using: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // Resume Playing }) – Paul N Mar 15 '12 at 21:33
1

Did you look at this link to handle audio interruptions. You need to setup AVAudioSessionDelegate and handle the interruptions.

rydgaze
  • 1,050
  • 13
  • 24