Set ContentURL of MPMoviePlayerController twice

2019-05-15 19:09发布

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!

2条回答
The star\"
2楼-- · 2019-05-15 19:28

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];
查看更多
Deceive 欺骗
3楼-- · 2019-05-15 19:42

The documentation for the contentURL property states the following:

If you set this property while a movie is playing, that movie pauses and the new movie begins loading. The new movie starts playing at the beginning.

So what you're experiencing isn't the expected behaviour. You may want to retrieve and check the error log for the MPMoviePlayerController using its errorLog property.

查看更多
登录 后发表回答