如何以编程方式退出MPMoviePlayerController?

时间:2013-06-17 10:05:14

标签: iphone ios objective-c cocoa-touch mpmovieplayercontroller

我需要在播放时以编程方式退出MPMoviePlayerController,而不是按完成按钮。可能吗。有没有办法模拟完成按钮点击?

1 个答案:

答案 0 :(得分:4)

我有一个技巧给你。您可以在uiview上使用mpmovieplayer,然后在停止播放器后删除uiview

在ViewController.h中

      MPMoviePlayerController *moviePlayerController;
      UIView *view1;
    -(IBAction)cancelPlay:(id)sender;

在ViewController.m中

  - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *filepath = [[NSBundle mainBundle] pathForResource:@"try" ofType:@"mp4"];
        NSURL *fileURL = [NSURL fileURLWithPath:filepath];
        view1=[[UIView alloc]initWithFrame:CGRectMake(0, 10, 320, 300)];
        view1.backgroundColor=[UIColor blueColor];
        [self.view addSubview:view1];
        moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
        [moviePlayerController.view setFrame:CGRectMake(0, 10, 320,300)];
        [view1 addSubview:moviePlayerController.view];
        moviePlayerController.fullscreen = YES;
        moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
        [moviePlayerController play];
    }
    -(IBAction)cancelPlay:(id)sender{
        [moviePlayerController stop];
        [view1 removeFromSuperview];
    }