Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerController background audio issue in iOS5

I have an App that does the pretty standard operation: It plays audio (streamed or in filesystem) when the app is in 1) Foreground mode, 2) Screen locked state 3)Background mode. This was working fine in all iOS prior to iOS5.

I have been using MPMoviePlayerController (Because it can play streamed and local file system audio) I have the following setup:

  1. info.plist has Background Mode set to "Audio"
  2. I have Audiosession setup as shown at http://developer.apple.com/library/ios/#qa/qa1668/_index.html

    NSError *activationError = nil;
    AVAudioSession *mySession = [AVAudioSession sharedInstance];
    [mySession setCategory: AVAudioSessionCategoryPlayback error: &activationError];
    if (activationError) { /* handle the error condition */ }
    [mySession setActive: YES error: &activationError];
    if (activationError) { /* handle the error condition */ }
    
  3. I have background timer enabled that gets stopped at the end of audio playback UIBackgroundTaskIdentifier newId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

  4. I have the Moveplayer's useApplicationAudioSession = NO
  5. I have subscribed to the following events to detect and handle various playback state and to start a new audio file at the end of current file. MPMoviePlayerLoadStateDidChangeNotification MPMoviePlayerPlaybackDidFinishNotification MPMoviePlayerPlaybackStateDidChangeNotification MPMoviePlayerNowPlayingMovieDidChangeNotification

Problem:

With this the audio starts to play and when the application is put to background state or if the phone is locked, the audio continues to play. But, after when I start another audio file, I start getting PlaybackDidFinishNotification immediately with the state set to Playback ended (But the file was never played)

The same code plays audio files in foreground mode (After the current audio file ends, the next file is started without any problem)

Is there anything new in iOS5 I should be doing to get this to work? I read through the MPMoviePlayerController class reference and I couldn't see anything specific for iOS5.

Thanks in advance.

like image 342
rydgaze Avatar asked Nov 17 '11 01:11

rydgaze


1 Answers

Finally figured out the issue. This is solved in this post in apple dev forums (needs login to see). That post was applicable to AVPlayer but also fixes the problem with MPMoviePlayerController as well.

Basically, this is an excerpt from that post:

your app must support remote control events! These are the audio controller interface prex/nex/play/pause on the left of the multitask switcher taskbar (not sure about the proper name of the thing). You to this ensuring your view becomes First Controller and then calling

> [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

in viewDidLoad. Once you do this, your Player will no longer return NO!!

like image 188
rydgaze Avatar answered Nov 20 '22 08:11

rydgaze