视频播放后如何更改视图?

时间:2010-09-06 21:37:03

标签: iphone objective-c xcode

我想制作一个应用程序,当我按下一个按钮时,一个视频开始播放,当它完成或者当我按下“完成”按钮时,它应该带我到一个不同于我的第一个视图发布了视频。

更新

这是我正在使用的代码,但它不起作用。我需要使用MPMoviePlayerViewController而不是MPMoviePlayerController。有什么想法吗?

NSBundle *Bundle = [NSBundle mainBundle];
    NSString *moviePath = [Bundle pathForResource:@"video1" ofType:@"m4v"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];


- (void) movieFinishedCallback:(NSNotification*) notification {
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:moviePlayer];    
    [moviePlayer release];
    // dismiss your view or present a new view here.
    View1 *View1b = [[View1 alloc] initWithNibName:nil bundle:nil];
    View1b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController: View1b animated:YES];
}

1 个答案:

答案 0 :(得分:0)

除非您需要非常自定义的东西,否则MPMoviePlayerController可能会满足您的需求。您可以将其添加到视图或较小的子视图中,并且可以禁用全屏等控件。该URL可以是本地文件或删除资源。

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player view].frame = [myView bounds];
[myView addSubview: [player view]];
[player play];

观察MPMoviePlayerPlaybackDidFinishNotification以确定何时完成,并从该观察者的块或所选方法中,您可以关闭视图或呈现新视图。

[[NSNotificationCenter defaultCenter] 
        addObserver:self
           selector:@selector(movieFinishedCallback:)                                                 
               name:MPMoviePlayerPlaybackDidFinishNotification
             object:player];

然后

- (void) movieFinishedCallback:(NSNotification*) notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] 
        removeObserver:self
                  name:MPMoviePlayerPlaybackDidFinishNotification
                object:player];    
    [player release];

    // dismiss your view or present a new view here.
}