Search code examples
cocoacocoa-touchmpmovieplayercontrollermedia-playermpmovieplayer

Set ContentURL of MPMoviePlayerController twice


I create an embedded MPMoviePlayerController thusly inside my loadView method:

self.moviePlayerController = [[[MPMoviePlayerController alloc] init] autorelease];

// add to view, setup moviePlayerController's view frame, etc

And I can later load a movie the user chooses thusly:

NSURL *fileUrl = ...
self.moviePlayerController.contentURL = fileUrl;

and everything works great.

However, if I set the contentURL again:

NSURL *fileUrl2 = ... self.moviePlayerController.contentURL = fileUrl2;

This does not work, even if fileUrl2 == fileUrl1.

When I change the contentURL, I get the following playbackState and loadState:

After first setContentURL:

loadState == playable | playthroughOK

playbackState == playing

After my second setContentURL:

playbackState == stopped

loadState == unknown

I can of course create a new MPMoviePlayerController for every movie, but I want to make sure this issue isn't indicative of a larger problem.

Thanks!


Solution

  • In my initial version, I was only allowing movies to be played via the embedded controls. If I forced the movie to start playing immediately after setting the contentURL, everything worked fine:

    self.moviePlayerController.contentURL = fileUrl;
    [self.moviePlayerController play];
    

    However, this is not the behavior I wanted. I discovered that when

    -[MPMoviePlayerController play]
    

    is called,

    -[MPMoviePlayerController prepareToPlay]
    

    is called automatically. Apparently, prepareToPlay must be called in order for the embedded controls and initial frame of the movie to show. It seems to be called automatically the first time setContentURL is called.

    So, I just changed my setContentURL call to the following, and everything worked.

    self.moviePlayerController.contentURL = fileUrl;
    [self.moviePlayerController prepareToPlay];