视频播放后视图无法返回

时间:2013-05-12 22:23:26

标签: ios

我正在运行以下代码。视频播放效果很好但是在它完成之后它只是一个黑色的srcreen,我的原始视图永远不会回来。当我点击黑屏时,我只看到消息“正在加载......”有人可以解释一下我做错了什么。感谢

- (IBAction)video:(UIBarButtonItem *)sender
{
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                             pathForResource:@"IMG_0973" ofType:@"MOV"]];
        moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];


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



        moviePlayer.controlStyle=MPMovieControlStyleDefault;
        //moviePlayer.shouldAutoplay=NO;
        [moviePlayer play];
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES animated:YES];

    }

}

- (void) moviePlayBackDonePressed:(NSNotification*)notification
{
    [moviePlayer stop];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];


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

    moviePlayer=nil;
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    [moviePlayer stop];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

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

3 个答案:

答案 0 :(得分:1)

添加此通知方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:player];

在加载影片后调用此方法,在此方法中添加moviePlayer视图。

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

   moviePlayer.controlStyle=MPMovieControlStyleDefault;
   [self.view addSubview:moviePlayer.view];
   [moviePlayer play];
   [moviePlayer setFullscreen:YES animated:YES];

}

答案 1 :(得分:0)

在您的视频IBAction中,您需要先添加子视图,然后才能告诉它。切换行[moviePlayer play]和[self.view addSubview:moviePlayer.view]。让我们知道它有效!实际上,即使在全屏幕后,您也可能需要放置moviePlayer。

答案 2 :(得分:0)

i think this will help you .....


 -(void)playVideo
{

    NSString *contentURL = [[NSBundle mainBundle] pathForResource:@"xyz" ofType:@"mp4"];

    MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];

    if (moviePlayerViewController)
    {
        [moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];

        [moviePlayerViewController.moviePlayer setFullscreen:YES];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];

        [moviePlayerViewController.moviePlayer play];

        [navi presentModalViewController:moviePlayerViewController animated:NO];

        [moviePlayerViewController release];

        moviePlayerViewController  = nil;
    }

}
-(void)MovieFinished:(NSNotification *)notification
{
    MPMoviePlayerController *player = (MPMoviePlayerController *)notification.object;

   [player stop];


    [[NSNotificationCenter defaultCenter] removeObserver:self];

//do rest of the stuff


}