user359519
user359519

Reputation: 711

MPMoviePlayerController plays after canceling in 3.1.2

I have encountered a problem with the MPMoviePlayerController in 3.1.2.

If I cancel the player while it is still loading, the player closes. However, the video starts playing a few moments later in the background. The only ways to stop it are to play another video or close the app. This seems to work fine in 3.2+.

Here's what I'm doing:

- (void)loadMoviePlayer
{
    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil];

    if ([NSClassFromString(@"MPMoviePlayerController") instancesRespondToSelector:@selector(view)])
    {

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
        // running iOS 3.2 or better
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.mysite.com/myvideo.m3u8"]];
        [moviePlayer.view setBackgroundColor:[UIColor blackColor]];
        [moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
        //      [moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleNone];
        [self presentMoviePlayerViewControllerAnimated:moviePlayer];
        [moviePlayer.moviePlayer prepareToPlay];    
        [moviePlayer.moviePlayer play]; 
#endif
    }
    else 
    {
        MPMoviePlayerController *mMPPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.mysite.com/myvideo.m3u8"]];
        mMPPlayer.scalingMode=MPMovieScalingModeFill;
        mMPPlayer.backgroundColor=[UIColor blackColor];
        [mMPPlayer play];
    } 

}

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    // Remove observer
    [[NSNotificationCenter  defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification 
     object:nil];

    [self dismissModalViewControllerAnimated:YES];
}

I added moviePlayBackDidFinish this morning. It gets called when I hit cancel, but dismissModalViewControllerAnimated doesn't seem to do anything. I also tried removeFromSuperView, but my player will not respond.

So, how can I make sure the player does not play after hitting "cancel"?

Thanks in advance.

Upvotes: 2

Views: 2353

Answers (2)

Martin
Martin

Reputation: 2300

In my case, I found that setting the following line would eventually stop the movie player from playing:

moviePlayer.contentURL = nil;

(with moviePlayer your instance of MPMoviePlayerController).

Upvotes: 4

Till
Till

Reputation: 27597

You may have come across an old bug in MPMoviePlayerController. Back in the days, we actually had to play an almost empty (black, silence) M4V after playing proper content to be sure the player does not attempt to continue playback in the background when stopping at certain stages. That bug manifests in audible sound but no picture of the aborted/stopped video.

There are however a few more things worth trying when stopping (assuming your instance of MPMoviePlayerController is called moviePlayer);

  • set the current playback position to the complete movie duration moviePlayer.currentPlaybackTime = moviePlayer.duration;
  • send another stop within your notification handler [moviePlayer stop];

Upvotes: 5

Related Questions