2

我正在使用 iOS 6 中包含的媒体播放器框架来尝试从应用程序中播放电影。我导入然后:

-(IBAction)playMovie:(id)sender
{
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];
}

调用此函数时,视图将进入空白屏幕并无限加载。我已经尝试了这个实现的许多其他版本,结果各不相同,都失败了。特殊情况下的日志是:

2012-11-05 21:19:27.900 [MPAVController] Autoplay: Disabling autoplay for pause
2012-11-05 21:19:27.902 [MPAVController] Autoplay: Disabling autoplay
2012-11-05 21:19:27.977 [MPAVController] Autoplay: Disabling autoplay for pause
2012-11-05 21:19:27.978 [MPAVController] Autoplay: Disabling autoplay
2012-11-05 21:19:27.984 [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2012-11-05 21:19:28.156 [MPAVController] Autoplay: Enabling autoplay

对原因有想法吗?这是我第一次尝试播放视频,在这一点上它已成为一场噩梦。

4

3 回答 3

9

在 .h 文件中添加以下内容

@property (nonatomic, strong) MPMoviePlayerController *controller;

试试这个

 -(IBAction)playMovie:(id)sender
    {
        NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"];
        NSURL *fileURL = [NSURL fileURLWithPath:filepath];
        MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
        [self.view addSubview:moviePlayerController.view];
        moviePlayerController.fullscreen = YES;
    [moviePlayerController prepareToPlay];
        [moviePlayerController play];
[self setController:moviePlayerController];
    }
于 2012-11-06T13:11:07.090 回答
0
Am facing the same issue, you can else try playing it on the web view. 

   NSURL *url = [NSURL fileURLWithPath:self.filePath];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.docWebView loadRequest:request];
于 2013-01-17T02:55:51.587 回答
0

您的代码的问题是变量“moviePlayerController”只是局部范围,所以在您调用[moviePlayerController play];并退出函数后,局部变量被释放(因为这里有一个异步操作,播放方法可以从队列中播放)。它不再让内容引用 URL。因此,您会看到黑屏和不定式“正在加载...”

您需要声明一个类的实例变量并将内容从局部变量复制到类的属性,如上面@iAppDeveloper 的示例代码。它应该工作!

于 2014-08-14T04:24:07.100 回答