Unable to detect MPMoviePlayerDidExitFullscreenNotification

In my project, I used MPMoviePlayerController to stream video from http-url. He plays in full screen. When a video plays, if you click Finish, the video stops and it disappears, but the problem is that: if you click to close the video screen, the video screen will disappear, but it still plays, the video sound continues to play .

I tried to detect a full-screen exit notification and manually stop the video, but that did not work. My moviePlayerDidExitFullScreen method moviePlayerDidExitFullScreen not been called.

To control this, if I get the notifications correctly, I tried to get another notification: MPMoviePlayerPlaybackStateDidChangeNotification , and it works. It calls the method when the video starts.

I searched a lot of Apple forums and documentation, but I could not find enough information.

Here is my code to open full screen video and detect full screen output:

 - (void)openFullVideo { NSString* path = @"http://trtvizyon.mysys.com/test/leyla_ile_mecnun.mp4"; NSURL *fileURL = [NSURL URLWithString:path]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil]; player.controlStyle = MPMovieControlStyleDefault; player.movieSourceType = MPMovieSourceTypeStreaming; [self.view addSubview:player.view]; [player setFullscreen:YES animated:YES]; [player play]; } - (void) moviePlayerDidExitFullScreen:(id)sender { NSLog(@"moviePlayerDidExitFullScreen"); } 
+6
source share
1 answer

Well, I played with your code for a while, and finally removed this small bug in the gut.

Your first problem is not to save the player object (if you use ARC, if not, skip this). So just make sure you save it as an instance variable, for example:

 //Header File @interface ViewController : UIViewController { MPMoviePlayerController* _player; } // Implementation File - (void)openFullVideo { // ... _player = player; } 

Now, if it just works, then great !! But I get a terrible unresolved error on the side of the apple:

An AVPlayerItem can occupy only one position in a player queue at a time

To solve this problem, do it like this:

 NSString* path = @"http://trtvizyon.mysys.com/test/leyla_ile_mecnun.mp4"; NSURL *fileURL = [NSURL URLWithString:path]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil]; player.controlStyle = MPMovieControlStyleDefault; player.movieSourceType = MPMovieSourceTypeStreaming; [self.view addSubview:player.view]; [player setContentURL:fileURL]; [player setInitialPlaybackTime:-1.f]; [player setFullscreen:YES animated:YES]; [player prepareToPlay]; [player play]; _player = player; 

That should do it!

Other friendly tips:

  • Before playing the movie, make sure you delete yourself from NSNotificationCenter.
  • I would suggest adding something like if (_player != nil) to avoid re-creating the object.
+7
source

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


All Articles