Question

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");
}
Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top