0

我正在运行以下代码。视频播放正常,但完成后它只是进入黑色屏幕,我的原始视图再也没有回来。当我点击黑屏时,我只看到消息“正在加载.....”有人可以解释一下我做错了什么吗?谢谢

- (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];
    }
}
4

3 回答 3

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];

}
于 2013-05-13T07:16:07.460 回答
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


}
于 2013-05-13T05:32:17.877 回答
0

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

于 2013-05-12T22:47:04.483 回答