3

I am making an iPad app (iOS7, XCode5).

I am playing a HLS stream (m3u8) that contains video and audio. When the device goes to the background it stops playing. Is there a way to detach the audio from the video and just play the video while it is in the background? It seems Apple does not want you playing video in the background.

Any ideas? Thanks.

Jonovono
  • 3,437
  • 5
  • 42
  • 64
  • You can only play audio files (mp3, wav etc) and cannot play video files in background. Playing vids in background doesn't make sense. What's the point if no one can see them? If you are trying to just play the sound then you need to either get the sound file out of the movie or play a static audio file – Sam B Jan 28 '14 at 19:56
  • Ya, I know does not make sense. It's just our video/audio stream is an HLS stream so as far as I know I can't just take the audio from that. – Jonovono Jan 28 '14 at 20:02
  • This link shows how you can play audio in the background. http://stackoverflow.com/questions/15540726/ios-mpmovieplayercontroller-playing-audio-in-background – Tom Jan 28 '14 at 20:27
  • http://stackoverflow.com/questions/13023241/continue-playing-movie-in-mpmovieplayerviewcontroller-in-background/21752593#21752593 I have solved This issue in that way.Thanks – Tunvir Rahman Tusher Feb 13 '14 at 11:17

1 Answers1

5

It's possible but not without a bit of a skip in playback - the video will stop automatically, but since your app is basically still running in the background you are able to start the video manually again:

In your view controller that manages the player add:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

Then add the following method:

- (void)applicationDidEnterBackground:(NSNotification*)note {
    if (!_callCenter.currentCalls && self.currentPlayer.continuePlaybackInBackground == YES) {
        double delayInSeconds = 0.1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            // call play on your MPMoviePlayerController
        });
    }
}

I found that I had to use dispatch_after because it otherwise it would try to start the movie playback again before it had been stopped by the OS.

runmad
  • 14,846
  • 9
  • 99
  • 140
  • Thanks. But hmm, I can't seem to get this working with MPMoviePlayerController. It does not have a togglePlaybackToState that I can tell? Am I missing something – Jonovono Jan 31 '14 at 21:48
  • It was an example, I've removed the code and replace it with a comment. You need to just call `play` on your MPMoviePlayerController instance here. – runmad Feb 04 '14 at 15:03
  • Use AVSession for Background Music (Even you using MPMovePlayerController to play video or audio). If you want to use control center buttons also, use Remote Control Events. Remember to add [[UIApplication sharedApplication]beginReceivingRemoteControlEvents] in ViewDidLoad. – khunshan Aug 06 '14 at 11:45