我使用MPMoviePlayerController播放存储在ios6文档的子文件夹中的视频,但它不起作用



>我使用 MPMoviePlayerController 播放存储在 ios1 文档中名为 test6 的子文件夹中的视频,但它不起作用

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];//
NSString *movieFolderPath = [documentDirectory stringByAppendingPathComponent:@"/test1"];
NSString *newsrc = [[NSString alloc]initWithFormat:@"/%@",new.src ];
NSString *moviePath   = [movieFolderPath stringByAppendingPathComponent:newsrc];
NSURL *url = [[NSURL alloc]initWithString: [moviePath  stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] ];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
_moviePlayer.movieSourceType = MPMovieSourceTypeFile;
_moviePlayer.view.frame = new.region;
[_moviePlayer prepareToPlay];
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];

好 :url 是/用户/风苏/图书馆/应用程序支持/iPhone 模拟器/6.0/应用程序/53C97346-C8FD-42DF-AD37-F2B14D5D3984/文档/测试1/v1.mp4

我觉得网址有问题,但我不知道为什么,请帮帮我,非常感谢

问题在于如何构造NSURL对象。

MPMoviePlayerController希望指向本地文件的 URL 具有file://前缀。

执行此操作的方法如下:

NSString *filename = @"your_video.mpeg";
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *yourVideoPath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:yourVideoPath isDirectory:NO];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Rest of the code...

使用[NSURL fileURLWithPath:moviePath]这用于从文件中打开 URL。

最新更新