MPMoviePlayerViewController 可以工作,MPMoviePlayerController 不能..为什么?
我有这段代码,
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filepath];
//part 1
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player.view setFrame: self.view.bounds];
//[player prepareToPlay];
//[player setShouldAutoplay:YES];
//[player setControlStyle:2];
[self.view addSubview: player.view];
[player play];
//part2
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
[self presentMoviePlayerViewControllerAnimated:mp];
第 2 部分可以工作,第 1 部分则不行...他们使用相同的 url,我几乎从
我使用 ios5 的ADC 网站复制并粘贴了代码片段
I have this code
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filepath];
//part 1
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player.view setFrame: self.view.bounds];
//[player prepareToPlay];
//[player setShouldAutoplay:YES];
//[player setControlStyle:2];
[self.view addSubview: player.view];
[player play];
//part2
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
[self presentMoviePlayerViewControllerAnimated:mp];
the part2 works, part1 not...they are using the same url and I've almost copyed and pasted the snippet from the ADC site
I'm using ios5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题已经发布很长时间了,但答案是您必须保留 MPMoviePlayerController 变量的引用。
如果函数中有第 1 部分的代码,请在 .h 文件中声明
MPMoviePlayerController *player
。如果您不这样做(并且您正在使用 ARC 进行开发),您的玩家变量将在退出函数后立即被释放。请注意,您将player.view
添加到self.view
,因此玩家的视图会被保留,但玩家的控制器不会,并且会被释放。所以你的 .h 文件中应该有:
在这种情况下,你的函数必须类似于:
It has been a long time since this question was posted, but the answer is that you must keep a reference of the
MPMoviePlayerController
variable.If you have the code of part1 in a function, declare
MPMoviePlayerController *player
in your .h file. If you don't do that (and your are developing withARC
) your player variable will be deallocated as soon as you exit the function. Notice that your are addingplayer.view
toself.view
, so the player's view gets retained, but the player's controller does not and it gets deallocated.So your should have in your .h file:
and in this case, your function must be something like:
我在 iOS 5 上使用 MPPlayerController 遇到了类似的问题,我检查了 Apple 的示例项目,区别只是设置框架,所以我手动设置框架,它工作得很好。
I had a similar issue on iOS 5 with MPPlayerController and I have checked Apple's example project, the difference was only setting frame so I set frame manually and it worked perfectly.
在视频未加载之前,您无法将播放器视图添加到主视图。
因此,您应该将这一行 : 替换
为这一行 :
并添加此方法:
You can't add the player view to the main view until the video is not loaded.
So you should replace this line :
By this one :
And add this method: