Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not detect MPMoviePlayerDidExitFullscreenNotification

In my project, I used MPMoviePlayerController to stream video from an http url. It plays fullscreen. When the video is playing, if you tap on "Done" button the video stops and it disappears, but the problem is; if you pinch to close video screen the video screen disappears but it still plays, sound of video continues to play.

I tried to detect exit fullscreen notification and manually stop the video but it didn't work. My moviePlayerDidExitFullScreen method didn't called.

To control that if I am getting the notifications on the right way I tried to get another notification : MPMoviePlayerPlaybackStateDidChangeNotification, and it is working. It calls the method on video launching.

I searched many forums and Apple documentations but I couldn't find enough information.

Here is my code to open a fullscreen video and detect exit fullscreen :

- (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");
}
like image 927
ozgunb Avatar asked Jul 14 '12 07:07

ozgunb


People also ask

How do I enter full screen mode?

In a browser on a Windows computer, you can enter fullscreen mode by pressing the F11 key. The key or method for entering fullscreen mode may vary in other programs.

How do I exit full screen mode?

Click the Maximize button (top right corner of the Navigation Toolbar) to leave full screen mode or right-click empty space on a toolbar and choose "Exit Full Screen Mode" or press the F11 key.

Does fullscreen API require user permission?

The HTML fullscreen API is a little different from other JS APIs that require permission, in that it doesn't ask permission before entering fullscreen, it asks forgiveness *after* entering fullscreen. Firefox's fullscreen approval dialog, which asks "forgiveness" rather than permission.


1 Answers

OK, I played with your code for a while, and finally shot that little bug in the gut.

Your first problem is not retaining the player object (assuming you are using ARC, if not, then skip this). So, just make sure you retain it as an instance variable for example:

//Header File
@interface ViewController : UIViewController {
    MPMoviePlayerController* _player;
}

// Implementation File
- (void)openFullVideo {
    // ...
    _player = player;
}

Now, if that just works, then great!! But I am getting a dreaded unsolved bug on apple's side:

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

To solve this issue, do it like so:

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!

Some Other Friendly Advice:

  • Make sure you remove yourself from NSNotificationCenter before playing the movie again.
  • I would suggest adding something like if (_player != nil) to avoid recreating the object.
like image 55
Mazyod Avatar answered Oct 15 '22 08:10

Mazyod