Response to MPMoviePlayerController notifications during background media playback

I have an application that transfers video from a network and plays it using the MPMoviePlayerController object to play on the device or through AirPlay.

The application supports background control and has an “audio” parameter specified within the required UIBackgroundModes key in its plist file.

When playing AirPlay, the application can be successfully transferred to the background, and the video continues to play correctly. So far so good.

According to Apple documentation :

Turning on the sound key tells the system frameworks that they should continue playing and make the necessary callbacks in the application at appropriate intervals. If the application does not include this key, any sound when playing the application stops when the application moves to the background.

However, these callbacks are not performed.

The application uses two types of callbacks: those associated with MPMoviePlayerController and AVPlayer notifications MPMoviePlayerController sent during playback along with timer callbacks that control the playback position and performance statistics for monitoring purposes.

Looking at Apple's notes, I would certainly expect to get the first type of callback so that the application can respond to MPMoviePlayerPlaybackStateDidChangeNotification , MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerLoadStateDidChangeNotification , but this does not happen.

Does anyone know if they can be obtained during background playback of AirPlay, and if so, how was this achieved?

** Please note: the application works correctly when launched in the foreground and receives a fine notification. Only when you click on the background and play AirPlay notifications are not accepted.

Similarly, video plays over AirPlay in the background. These are only notifications that are not accepted **

+6
source share
3 answers

For completeness, I must add that at present I believe that there is no solution to this problem.

I discussed this directly with Apple through technical support, and there was no practical work.

This feature was necessary so that the application could record statistics about the playback of the stream at regular intervals. Although this is normal when video is playing on the device’s screen and above AirPlay, when the application is in the foreground, this cannot be done with the application in the background.

The solution I went with was to turn off the idle timer during all types of playback and turn it on again with:

 [UIApplication sharedApplication].idleTimerDisabled = YES; 

and

 [UIApplication sharedApplication].idleTimerDisabled = NO; 

Although this is not a solution to the original question, it is a workaround to prevent the problem in the first place.

0
source

I had this problem and fixed it, although it was a few months ago. I could send you my whole class to play a movie if that doesn't work. Please note that the navigation controller model is used.

NOTE. This is tested on an iPad 2 not on an iPhone.

I show my VC as follows:

  - (IBAction)playMovie:(id)sender { MovieVC* movController = [[MovieVC alloc] initWithID:2]; movController.view.backgroundColor = [UIColor blackColor]; AppDelegate *appDel = [[UIApplication sharedApplication] delegate]; [appDel.navigationController pushViewController:movController animated:NO]; [movController release]; } 

Then, in my MovieVC view control class, I set up video playback as follows:

 - (void)initMoviePlayer { mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]]; mMoviePlayer.allowsAirPlay = YES; mMoviePlayer.view.frame = [self.view bounds]; mMoviePlayer.view.backgroundColor = [UIColor clearColor]; mMoviePlayer.shouldAutoplay = YES; mMoviePlayer.fullscreen = YES; mMoviePlayer.scalingMode = MPMovieScalingModeAspectFit; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:mMoviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; } 

This is fixed for me if it does not correct comments and invalid changes with the entire class file.

+4
source

In one of my projects, I did what you did, and it worked for me. Two differences in my project:

  • I do not broadcast via airPlay (device playback only),
  • I just play audio files.

My step by step:

  • Add the audio file in UIBackgroundModes to the plist file,
  • Register at NotificationCenter for MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerPlaybackStateDidChangeNotification with the following code:

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackEnded:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 

It works like a charm.

+2
source

Source: https://habr.com/ru/post/907348/


All Articles