Can not detect MPMoviePlayerDidExitFullscreenNotification
Asked Answered
T

1

6

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");
}
Tobytobye answered 14/7, 2012 at 7:54 Comment(0)
T
7

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.
Twink answered 14/7, 2012 at 9:7 Comment(2)
Sorry, I fixed a few typos there. Also, I tested this code on iOS simulator 4.3 & 5.1 and it works on both.Twink
Thank you very very much, I have been working longs days for this issue. I tried to define MPMoviPlayerController on interface but I got error "An AVPlayerItem can occupy only one position in a player's queue at a time" like you said. Defining "_player = player" solved it I think. Thank you again...Tobytobye

© 2022 - 2024 — McMap. All rights reserved.