加载新视图时播放视频

时间:2012-03-27 18:52:05

标签: iphone ios xcode video


我正在开发一个iPhone应用程序,我遇到了一个非常小的问题。基本上,当我加载某个视图时,我希望播放视频。当视频播放完毕后,它将返回带有菜单和一些选项的视图。其中一个选项是重播视频。目前我有重播视频选项正常工作,但在加载视图时我无法播放视频。

我实现了playMovie和moviePlayBackDidFinish方法。然后我将[self playMovie]放在viewDidLoad方法中,认为它最初会调用pl​​ayMovie方法,因此在视图加载时播放电影,但它似乎不起作用。

如果有人能解释为什么这种思维方法不起作用,也是一种正确的方法,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

我会尝试使用viewDidAppear而不是viewDidLoad,具体取决于你的viewController。视图通常只能加载一次,并且您可能在后台加载时浪费它。

此外,这是一部示例电影“完成方法”,我想知道你是否发布了一些你不应该发布的内容,或者从不告诉电影正确停止:

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 
    switch ([reason integerValue]) 
    {
            /* The end of the movie was reached. */
        case MPMovieFinishReasonPlaybackEnded:
            /*
             Add your code here to handle MPMovieFinishReasonPlaybackEnded.
             */
            break;

            /* An error was encountered during playback. */
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"An error was encountered during playback");
            [self performSelectorOnMainThread:@selector(displayError:) withObject:[[notification userInfo] objectForKey:@"error"] 
                                waitUntilDone:NO];
            [self removeMovieViewFromViewHierarchy];
            [self removeOverlayView];
            [self.backgroundView removeFromSuperview];
            break;

            /* The user stopped playback. */
        case MPMovieFinishReasonUserExited:
            [self removeMovieViewFromViewHierarchy];
            [self removeOverlayView];
            [self.backgroundView removeFromSuperview];
            break;

        default:
            break;
    }
}