Set ContentURL of MPMoviePlayerController twice

丶灬走出姿态 提交于 2019-12-07 17:04: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!


回答1:


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];



回答2:


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.



来源:https://stackoverflow.com/questions/5383821/set-contenturl-of-mpmovieplayercontroller-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!