Jebadiah
Jebadiah

Reputation: 65

MPMoviePlayerController ends immediately

I'm trying to load a 25-second mp4 movie from my resource file, but when I play it, my MPMoviePlayerPlaybackDidFinishNotification selector is called immediately with MPMovieFinishReasonPlaybackEnded. When I log my playback state it shows this:

MPMoviePlaybackStatePlaying
MPMoviePlaybackStatePaused
MPMoviePlaybackStateStopped
MovieFinishReasonPlaybackEnded
MPMoviePlaybackStatePlaying
MPMoviePlaybackStatePaused

even though I only call the play method once. I hope someone can help me.

-- Edited to show my code:

MPMoviePlayerController* player = [[[MPMoviePlayerController alloc] initWithContentURL:movieURL] autorelease];

if (player)
{

    self.moviePlayerController = player;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:player];

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerPlaybackStateDidChange:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:player];


    player.contentURL = movieURL;

    player.movieSourceType = MPMovieSourceTypeFile;

    player.controlStyle = MPMovieControlStyleNone;

    player.fullscreen = YES;

    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            player.view.transform = CGAffineTransformMakeRotation(90.0f * (M_PI / 180.0f));
            break;
        case UIInterfaceOrientationLandscapeRight:
            player.view.transform = CGAffineTransformMakeRotation(-90.0f * (M_PI / 180.0f));
            break;
        default:
            break;
    }

    player.view.frame = self.view.bounds;

    [self.view addSubview:player.view];
}

[self.moviePlayerController play]

Upvotes: 2

Views: 679

Answers (2)

Martin Kenny
Martin Kenny

Reputation: 2488

Is self.moviePlayerController a retained property? If not, the MPMoviePlayerController instance will be released very quickly (by the autorelease), and you might get similar behaviour.

Upvotes: 1

Darren
Darren

Reputation: 10129

Without having any more of your code to look at, I would suggest trying to play another file that you know can play. For example, grab the movie from this sample project: http://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html and see if it plays.

I had something similar happen to me when I was trying to play a file that wasn't properly formatted.


Hmm... I don't see what's wrong. Are you sure that movieURL is correct? How do you get it?

For the record, here is how I present movies, although it wouldn't have quite the same effect as what you're doing.

NSString *path = [[NSBundle mainBundle] pathForResource:movieFileName ofType:@"m4v"];

// If path is NULL (the resource does not exist) return to avoid crash
if (path == NULL)
    return;

NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerViewController *mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
mpViewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
mpViewController.moviePlayer.shouldAutoplay = YES;

// NOTE: This can sometimes crash the app in the Simulator. This is a known bug
// in xcode: http://stackoverflow.com/a/8317546/472344
[self presentMoviePlayerViewControllerAnimated:mpViewController];
[mpViewController release];

Upvotes: 0

Related Questions