无法从 iOS 中的文档目录播放电影
Posted
技术标签:
【中文标题】无法从 iOS 中的文档目录播放电影【英文标题】:Unable to play movie from document directory in iOS 【发布时间】:2013-01-17 05:40:02 【问题描述】:我有这个函数可以将 NSData 写入文档字典 -
- (void)writeData
NSString *str = @"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
NSLog(@"%@",path);
NSLog(@"About to Write Data");
NSData* data0 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
[data0 writeToFile:path atomically:YES];
NSLog(@"Writting Data Completed");
后来我阅读了这些数据并尝试使用 MPMoviePlayerController 播放视频,但不幸的是 不玩-
- (void)readData
NSString *str = @"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
NSLog(@"url:%@",url);
NSError *error = nil;
BOOL isReacahble = [url checkResourceIsReachableAndReturnError:&error];
NSLog(@"isReachable :%i,%@",isReacahble,error);
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileExist)
NSLog(@"File Exist in path");
else
NSLog(@"NO File Exist");
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer prepareToPlay];
NSLog(@"movie player:%@",moviePlayer);
[moviePlayer setControlStyle: MPMovieControlStyleFullscreen];
[moviePlayer.view setFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:moviePlayer.view];
moviePlayer.shouldAutoplay = YES;
[moviePlayer play];
当我尝试播放时,它显示以下错误-
2013-01-17 11:21:07.067 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-17 11:21:07.068 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay
2013-01-17 11:21:07.090 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.091 VideoCheck[338:907] movie player:<MPMoviePlayerController: 0x21078250>
2013-01-17 11:21:07.093 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.102 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.113 VideoCheck[338:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-17 11:21:07.114 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-17 11:21:07.115 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.121 VideoCheck[338:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2013-01-17 11:21:07.149 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
有人可以运行代码并提供一些关于这里发生的事情的信息吗?请有人帮助我。我正在使用 ios 6。
【问题讨论】:
我发现了问题,这完全是愚蠢的事情,如果我们使用带有大量“/”的大名称,UIKit 无法正确解析数据,并且我们必须需要另外 1 件事来使用 MPMoviePlayer 播放视频在 url 路径中使用 .m4v 等视频格式扩展名,否则 MPMoviePlayer 将无法播放。 【参考方案1】:在您的wrieData
方法中,不要将URL
名称作为文件,而是仅给出video
的名称。喜欢
NSString *path = [documentsDirectory stringByAppendingPathComponent:[str lastPathComponent]];
并在readData
方法中做同样的事情。
【讨论】:
以上是关于无法从 iOS 中的文档目录播放电影的主要内容,如果未能解决你的问题,请参考以下文章
如何从其他 iOS 应用程序读取我应用程序的 Document 目录中的文档?