MPMoviePlayer 不会在 [moviePlayer.view removeFromSuperview] 和 [moviePlayer release] 上消失
我的 MPMoviePlayerController 有问题。当我观看视频并点击左上角的“完成”按钮时,MoviePlayer 不会消失,即使代码似乎被称为:
NSURL *url = [NSURL URLWithString:article.enclosureLink];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Set movie player layout
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES];
// May help to reduce latency
[moviePlayer prepareToPlay];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieReadyToPlay:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
选择器:
- (void) movieReadyToPlay:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState == MPMovieLoadStatePlayable){
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
//moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer setFullscreen:NO animated:YES];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
NSLog(@"Finished movie!");
}
在我看来,这是一个非常简单的代码,但我一定犯了一个愚蠢的错误。 NSLog显示该函数被调用,但玩家停留在原处,没有办法摆脱它。
另外,该播放器在所谓的发布后仍在运行这一事实似乎表明存在根本性错误,我只是不明白是什么。
有人有建议吗?
[更新:] 奇怪的是,在 iPhone 模拟器中它运行得很好!
[更新2:] 我尝试并创建了一个特定的 UIviewcontroller,尽管这不是我想要的方式,因为动画不太好。但我了解到我也有同样的问题。似乎必须做点什么来解雇玩家,但它又重新开始了。
当我把 [self.moviePlayer setFullscreen:YESanimated:YES];在viewDidApear中,然后单击播放器中的“完成”按钮,当我点击“完成”按钮时,播放器视频会重新开始(再次调用viewDidAppear)。所以在我看来,有些东西被触发了,让视频重新开始。
如果我把它放在 viewDidLoad 中,那么系统就可以工作,但是图形是混合和混乱的......
任何帮助都非常非常感谢,因为我现在花了两天时间在这上面,而没有搞清楚它!
I have a problem with MPMoviePlayerController.When I am watching a video, and hit the 'Done' button on the left top, the MoviePlayer does not disappear, even though the code seems to be called:
NSURL *url = [NSURL URLWithString:article.enclosureLink];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Set movie player layout
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES];
// May help to reduce latency
[moviePlayer prepareToPlay];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieReadyToPlay:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
And the selectors:
- (void) movieReadyToPlay:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState == MPMovieLoadStatePlayable){
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
//moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer setFullscreen:NO animated:YES];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
NSLog(@"Finished movie!");
}
This looks to me a very straight forward code, but I must make a stupid mistake. The NSLog shows that the function is called, but the player stays where it is and there is no way of getting rid of it.
Also, the very fact that the player is still operational after the alleged release seems to indicate that there is something fundamental wrong, I just don't see what.
Is there anybody who has a suggestion?
[Update:]
Strangely in the iPhone Simulator it works fine!
[Update2:]
I tried and created a specific UIviewcontroller, even though it is not the way I want to do it as the animations are not nice. But what I learned is that I have the same problem. It seems to hav to do something with dismissing the player, but it starting again.
When I put [self.moviePlayer setFullscreen:YES animated:YES]; in viewDidApear, and click the 'Done' button in the player, the player, the video starts over again when I hit the Done button (the viewDidAppear is called again). So something is triggered, so it seems to me, to make the video start again.
If I put it viewDidLoad, then the system works, but the graphics are mixed and confused...
Any help is really, really appreciated as I spend two days on this now without making head or tail of it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,我尝试了所有这些:
<代码>
[电影播放器停止];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
但没有任何效果。我发现这与我的 MPMoviePlayerController 进入全屏有关。修复?
For me, I tried all of these:
[moviePlayer stop];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
And nothing worked. I figured out it had to due with my MPMoviePlayerController entering full screen. The fix?
添加
在之前
可能会起作用。
更新:
如果这不起作用,请在删除子视图之前尝试将 controlstyle 设置为 MPMovieControlStyleNone。大多数情况下,controlStyle 会导致此类问题。
Adding
before
may work.
Update:
If this doesn't work then try setting controlstyle to MPMovieControlStyleNone before removing the subview.Most of the time the controlStyle causes such problems.