iOS 8中AVPlayer无法加载视频

3
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:[[NSBundle mainBundle]
                                                       URLForResource:@"Besan"
                                                       withExtension:@"mp4"]];
[playerViewController.player play];

我正在尝试在iOS 8中使用AVPlayerViewController,但视频无法加载。我只会得到下面的屏幕。
整个代码库在这里https://github.com/sairamsankaran/AVPlayerDemo

enter image description here


不确定为什么您没有调试URL。 - onmyway133
3个回答

7
我已经下载了你的项目并查看了一下。解决方案十分简单,您忘记将视频文件包含在目标中,这意味着当您设置URL时,您指向的文件不存在,这意味着NSURL是无效的。
在Xcode中的实用程序侧边栏中,选择导航器中选定的mp4文件,在“文件检查器”中选择“目标成员资格”,确保选中您的应用程序目标。
完成此操作后,您的电影将在启动时播放。
(图片)

1
你最好从一个工作的例子开始,然后进行扩展。如果你愿意,可以尝试在我的 博客文章 中描述的 github 上使用 AVPlayerViewController 的工作 xcode 例子项目,这是一个展示太阳高清SDO视频的示例项目,它在新的视网膜iPad上看起来最好,因为视频质量非常高。

0

@runmad,我也用同样的方法完成了,但我正在尝试打开本地视频。这是我的代码

{
// Initialize the movie player view controller with a video URL string
        AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
        /*NSURL *url = [NSURL URLWithString:@"https://www.youtube.com/watch?v=qcI2_v7Vj2U"];
         playerViewController.player = [AVPlayer playerWithURL:url];*/
//        playerViewController.player = [AVPlayer playerWithURL: [ [filePath]URLForResource:@"Besan"
//                                                                withExtension:@"mp4"]];
        NSLog(@"\nFile URL\n%@",videosURL);
        AVPlayerItem* item=[[AVPlayerItem alloc]initWithURL:videosURL];
        NSLog(@"\n\nAVPlayerItem\n%@",item);
        playerViewController.player=[AVPlayer playerWithPlayerItem:item];
        [playerViewController.player play];
        [self presentViewController:playerViewController animated:YES completion:^{}];
}

我已经尝试过不使用AVPlayerItem,但结果相同(只有带有完成、播放、前进、后退按钮的黑屏)。

然后我尝试使用MPMoviePlayerController,但很快就被关闭了,没有显示视频。 以下是MPMoviePlayerController代码:

{
    MPMoviePlayerViewController   *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:videosURL];
        [self presentMoviePlayerViewControllerAnimated:moviePlayerController];
        [moviePlayerController.moviePlayer play];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接