如何解雇电影播放器​​视图

时间:2011-02-27 22:49:33

标签: iphone cocoa-touch mpmovieplayercontroller media-player

嘿,我正在尝试制作一个iPhone应用程序,你点击UITableView中的一个单元格,它会在你点击的任何内容上显示一个电影。到目前为止,我已经得到了:

  1. 拥有UITableView
  2. 单击单元格时加载nib文件
  3. 但我仍然需要帮助让电影消失并在完成后转到主nib文件

    我尝试过使用这样的通知:

    MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
        initWithContentURL:[NSURL fileURLWithPath:movpath]];
    [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(leave) userInfo:nil repeats:NO];
    

    这就是我在IOS 4.0上加载电影的方式

    -(IBAction)PlayMovie
    {
        NSString *movpath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"mov"];
        MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
                                         initWithContentURL:[NSURL fileURLWithPath:movpath]];
                                                            [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(leave) userInfo:nil repeats:NO];     
        [window addSubview:mpviewController.view];
        [window makeKeyAndVisible];
        MPMoviePlayerController *mp = [mpviewController moviePlayer];
        [mp prepareToPlay];
        mp.scalingMode = MPMovieScalingModeFill;
    
        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidExitFullscreen:)
        //                                           name:MPMoviePlayerDidExitFullscreenNotification
        //                                         object:mp];
    
        [[mpviewController moviePlayer] play];
    }
    

    请帮助

    〜感谢

1 个答案:

答案 0 :(得分:0)

这是你应该使用的代码模板,它应该在电影播放结束后工作和解散你的电影。

- (void)playMovie {
    NSString *path = [[NSBundle mainBundle] 
                      pathForResource:@"name" ofType:@"m4v"];

    player = [[MPMoviePlayerViewController alloc]
              initWithContentURL:[NSURL fileURLWithPath:path]];

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


    [self presentMoviePlayerViewControllerAnimated:player];

}


-(void) movieFinishedPlaying: (NSNotification *) note {
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification 
     object:[player moviePlayer]];

    [player release];
}