5

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

@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
4

1 回答 1

13

Assumed precondition: your project is using ARC.

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.

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

Header:

@instance SRViewController

   [...]

   @property (nonatomic,strong) MPMoviePlayerController *player;

   [...]

@end

Implementation:

@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
于 2013-02-25T11:25:29.750 回答