我有一个本地的iPhone兼容的MP4视频(我已经将视频转换为iPhone格式,并测试了将视频上传到web服务器并将其URL输入移动Safari,视频可以完美播放),我想使用MPMoviePlayerController
播放。
这是我的代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Welcome" ofType:@"mp4"];
NSURL *fileURL = [NSURL URLWithString:filePath];
playerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
playerController.movieSourceType = MPMovieSourceTypeFile;
playerController.backgroundView.backgroundColor = [UIColor whiteColor];
playerController.view.translatesAutoresizingMaskIntoConstraints = YES;
playerController.scalingMode = MPMovieScalingModeAspectFill;
//[playerController.view setFrame:moviePlayerContainerView.frame];
[playerController.view setFrame:CGRectMake(0, 0, 200, 200)];
[moviePlayerContainerView addSubview:playerController.view];
playerController.contentURL = fileURL;
playerController.fullscreen = YES;
[playerController prepareToPlay];
[playerController play];
fileURL
指向一个有效的文件:
(lldb) po fileURL
/var/mobile/Applications/73C97CC9-731E-4446-A53C-2A8CA30A2393/Tanisalim.app/Welcome.mp4
(lldb) p (BOOL)[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]
(BOOL) $0 = YES
当我尝试在模拟器上运行(无论是6.1还是7.1),它只会在框架中显示一个黑屏(如果全屏是NO
),或者打开一个电影播放器视图控制器并永远停留在“加载...”状态。当我在设备上运行(iOS 7.1)时,行为相同,但我在控制台上得到以下错误:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
当我尝试直接从网页加载相同的视频文件进行流媒体播放时,它完美地工作:
//playerController.contentURL = fileURL;
playerController.contentURL = [NSURL URLWithString:@"http://mywebsite.com/Welcome.mp4"];
我尝试了不同的视频文件,但结果都一样。我做错了什么?