为什么我的MPMoviePlayerController不会播放? [英] Why won't my MPMoviePlayerController play?

查看:110
本文介绍了为什么我的MPMoviePlayerController不会播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的代码来播放基本的.mov视频文件,但是当按下我指定了按钮的按钮时,唯一显示的是黑框,但没有播放视频。任何帮助表示赞赏。谢谢。

I'm trying to get a basic .mov video file to play using the code below, but when the button I have assigned the action is pressed, the only thing that shows up is the black frame, but no video is played. Any help is appreciated. Thanks.

@implementation SRViewController

-(IBAction)playMovie{
    NSString *url = [[NSBundle mainBundle]
                     pathForResource:@"OntheTitle" ofType:@"mov"];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
                                       initWithContentURL: [NSURL fileURLWithPath:url]];

    // Play Partial Screen
    player.view.frame = CGRectMake(10, 10, 720, 480);
    [self.view addSubview:player.view];

    // Play Movie
    [player play];
}

@end


推荐答案

假设前提条件:您的项目正在使用ARC

您的 MPMoviePlayerController 实例只是本地的,ARC无法告诉您需要保留该实例。由于控制器未被其视图保留,因此结果是 MPMoviePlayerController 实例将在执行 playMovie 方法执行。

Your MPMoviePlayerController instance is local only and ARC has no way of telling that you need to retain that instance. As the controller is not retained by its view, the result is that your MPMoviePlayerController instance will be released directly after the execution of your playMovie method execution.

要解决该问题,只需将播放器实例的属性添加到 SRViewController 类,并将实例分配给那个属性。

To fix that issue, simply add a property for the player instance to your SRViewController class and assign the instance towards that property.

标题:

@instance SRViewController

   [...]

   @property (nonatomic,strong) MPMoviePlayerController *player;

   [...]

@end

实施:

@implementation SRViewController

   [...]

    -(IBAction)playMovie
    {
        NSString *url = [[NSBundle mainBundle]
                         pathForResource:@"OntheTitle" ofType:@"mov"];
        self.player = [[MPMoviePlayerController alloc]
                                           initWithContentURL: [NSURL fileURLWithPath:url]];

        // Play Partial Screen
        self.player.view.frame = CGRectMake(10, 10, 720, 480);
        [self.view addSubview:self.player.view];

        // Play Movie
        [self.player play];
    }

    [...]

@end

这篇关于为什么我的MPMoviePlayerController不会播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆