0

我正在开发一个 iOS 应用程序,我想在用户按下按钮时播放视频。我已经开发了这个,用户按下按钮并播放视频,但是当视频结束时,视频的视图仍然存在,并且它被冻结在视频的最后一帧。

我在谷歌搜索,我发现了这个问题:

iOS 6,Xcode 4.5 视频播放完毕后不退出

我已经使用了那里编写的代码,但我还没有修复它。这是我的代码:

-(IBAction)reproducirVideo:(id)sender
{
NSURL *url5 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                      pathForResource:@"instrucciones"     ofType:@"mp4"]];
_moviePlayer =  [[MPMoviePlayerController alloc]
                  initWithContentURL:url5];

_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

-(void) moviePlayBackDidFinish:(NSNotification *)aNotification{
[_moviePlayer.view removeFromSuperview];
_moviePlayer = nil;
}


- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
[_moviePlayer stop];
[_moviePlayer.view removeFromSuperview];
_moviePlayer = nil;
}
4

1 回答 1

1

Sorry for the inconvenience but I have read this question and I just fixed it:

MPMoviePlayerController will not automatically dismiss movie after finish playing (ios 6)

This is the right code:

- (IBAction)reproducirVideo:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"instrucciones" ofType:@"mp4"]];
_moviePlayer =
[[MPMoviePlayerController alloc]
 initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:_moviePlayer];

_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

MPMoviePlayerController *player = [notification object];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:player];

if ([player
     respondsToSelector:@selector(setFullscreen:animated:)])
{
    [player setFullscreen:NO animated:YES];
    [player.view removeFromSuperview];
}
}

THANKS JASEN!!! YOU SAVED MY LIFE

Best regards

于 2013-07-05T09:29:56.747 回答