单击完成后,MPMoviePlayerController不会删除视图 [英] MPMoviePlayerController does not remove view when clicking done
问题描述
我正在创建一个MPMoviePlayerController对象,并以全屏模式流式传输视频.
I am creating a MPMoviePlayerController object and streaming a video in full screen mode.
我正在使用UIViewController来显示电影视图.
I am using a UIViewController to display the movie view.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE
NSURL *url = [NSURL URLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"];
MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
mPlayer.view.frame = gMainView.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mPlayer];
mPlayer.shouldAutoplay = YES;
mPlayer.controlStyle = MPMovieControlStyleFullscreen;
[gMainView addSubview:mPlayer.view];
[mPlayer prepareToPlay];
[mPlayer setFullscreen:YES animated:YES];
[mPlayer play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
//movie finished playing
}
else if (reason == MPMovieFinishReasonUserExited) {
//user hit the done button
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}
else if (reason == MPMovieFinishReasonPlaybackError) {
//error
}
}
单击完成后,视频画面从屏幕上移开,但控件从屏幕上移开,视图也没有从屏幕上移出.
When clicking done, the video visual is removed from the screen, but the controls are not removed from the screen and the view is not removed from the screen.
控件确实出现"//用户点击完成按钮".它确实执行了从超级视图中删除视图的代码,我通过添加日志进行了检查,但未从屏幕中删除控件,也未从屏幕中删除视图. 我在做什么错了?
The control does come to "//user hit the done button". It does execute the code to remove the view from superview, I checked by adding logs, but the controls are not removed from the screen and the view is not removed from the screen. What am I doing wrong?
如果我使用MPMoviePlayerViewController,那么它甚至都不会等我按完成".视频播放完毕后,它将自动删除视图.但是我不要.
If I use MPMoviePlayerViewController then it doesn't even wait for me to press Done. Once the video is complete it automatically removes the view. But I don' want that.
如果我删除"[mPlayer setFullscreen:是动画:是]",那么在单击完成"时,视图将被完全删除.但是视频无法全屏显示,并且状态栏变为灰色,这也是我不想要的.
If I remove "[mPlayer setFullscreen:YES animated:YES]" then when clicking on Done, the view is removed completely. But the video is not displayed in full screen and the status bar goes gray which is again what I don't want.
推荐答案
下面的代码对我有用,希望它对您也有帮助.
The below code worked for me, Hope it helps you too.
-(IBAction)playVedio:(id)sender{
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:mp];
}
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[mp.moviePlayer stop];
mp = nil;
[mp release];
[self dismissMoviePlayerViewControllerAnimated];
}
这篇关于单击完成后,MPMoviePlayerController不会删除视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!