如何在应用程序启动期间播放视频

时间:2012-08-08 10:53:07

标签: ios xcode mpmovieplayercontroller

我想知道如何在我的应用加载时播放视频,然后使用MPMoviePlayerController导航到ios应用中的下一页。我正在viewDidLoad加载视频。如何在视频播放完毕后转到下一页?

示例代码:

  - (void)viewDidLoad
  {
      NSLog(@"Welcome to Home Page");
      [super viewDidLoad];
      self.parentViewController.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-image-new.png"]];

      NSBundle * bundle =[NSBundle mainBundle];
      NSString * moviepath = [bundle pathForResource:@"opening_ani" ofType:@"mp4"];
      NSURL * movieurl = [[NSURL fileURLWithPath:moviepath]retain];
      MPMoviePlayerController * themovie = [[MPMoviePlayerController alloc]initWithContentURL:movieurl];
      themovie.view.frame=CGRectMake(0, 0, 1024, 768);
      [self.view addSubview:themovie.view];
      [themovie play];
      [themovie setShouldAutoplay:NO];
 }

1 个答案:

答案 0 :(得分:0)

<强>更新

我不是100%肯定你可以在应用程序加载时播放视频。有一个很好的解释here


在上面的视图控制器中,在调用[themovie play]之前,您可以注册播放完成的通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleNotification:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:nil];

在查看控制器时,您必须定义方法handleNotification:

    -(void)handleNotification:(NSNotification*)notification {
        if ( [notification.name isEqual:MPMoviePlayerPlaybackDidFinishNotification] ) {
            [[NSNotificationCenter defaultCenter] removeObserver:self
                                                            name:MPMoviePlayerPlaybackDidFinishNotification
                                                          object:nil];
            // move on to your next view here
        }
    }

More about notifications in iOS,一般来说非常有用。另外,here是关于MPMoviePlayer的所有详细信息,其中有一个部分列出了所有可能的通知及其详细信息。