Ios MPMoviePlayerController不';无法从文档文件夹播放

Ios MPMoviePlayerController不';无法从文档文件夹播放,ios,mpmovieplayercontroller,nsurl,nsdocumentdirectory,Ios,Mpmovieplayercontroller,Nsurl,Nsdocumentdirectory,绝望了。 大家好! 我与MPMoviePlayerController有一些问题。 我用NSBundle的视频制作了它。但那不是我需要的。我需要从Documents目录播放它,因为那是我存储录制视频的地方,URL存储在CoreData中。但是让我们把这一点放在一边,将代码简化到所需的最低限度。如果使用contentURL,这段代码实际上可以工作,因为它会导致NSBundle。之后,我要做什么才能到达目的地。 我所做的: NSURL *contentURL = [[NSBundle mai

绝望了。 大家好! 我与MPMoviePlayerController有一些问题。 我用NSBundle的视频制作了它。但那不是我需要的。我需要从Documents目录播放它,因为那是我存储录制视频的地方,URL存储在CoreData中。但是让我们把这一点放在一边,将代码简化到所需的最低限度。如果使用contentURL,这段代码实际上可以工作,因为它会导致NSBundle。之后,我要做什么才能到达目的地。 我所做的:

    NSURL *contentURL = [[NSBundle mainBundle] URLForResource:@"Oct_08_2012_10_00_51" withExtension:@"mp4"]; // this works
NSString* docPath = [NSSearchPathForDirectoriesInDomains
                     (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * docaPathFull = [NSString stringWithFormat:@"%@%@", docPath, @"/Oct_08_2012_10_00_51.mp4"];
NSURL * docUrl= [NSURL URLWithString: docaPathFull];
BOOL ex = [[NSFileManager defaultManager] fileExistsAtPath:docaPathFull];
NSLog(@"file exists: %d, path using docPath: %@",ex, docaPathFull);
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:docUrl];
player.shouldAutoplay = YES;
player.controlStyle = MPMovieControlStyleEmbedded;
[player.view setFrame: self.thumbButton.bounds];
[player prepareToPlay];
[self.view addSubview: player.view];
[player play];
我们所拥有的:

2012-10-08 13:14:43.532 Voto[11968:907] file exists: 1, path using docPath: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:43.907 Voto[11968:907] content URL: file://localhost/var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Voto.app/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:44.265 Voto[11968:907] doc URL: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:45.343 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay for pause
2012-10-08 13:14:45.344 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay
2012-10-08 13:14:46.518 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2012-10-08 13:14:46.540 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-08 13:14:46.554 Voto[11968:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-10-08 13:14:46.555 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-10-08 13:14:46.557 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-08 13:14:46.567 Voto[11968:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2012-10-08 13:14:46.871 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
所以,文件存在。。。 我已经看过的问题:

我还用类参考检查了ut,没有关于从文档播放的任何具体内容。 我的项目设置: 使用最新的iOS 6, 部署目标5.0 在iOS6 iPhone模拟器和配备iOS 6的iPad上进行测试。 如果我忘记添加内容,请提醒我,我会立即添加


求求你,救命!:)

如果您没有以正确的方式构建文件URL,您应该这样做:

NSString *docPath = [NSSearchPathForDirectoriesInDomains
                     (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:@"/Oct_08_2012_10_00_51.mp4"];
NSURL *docUrl= [NSURL fileURLWithPath:docaPathFull];
您应该使用
NSString
stringByAppendingPathComponent
方法将目录和文件添加到路径中; 另外,在创建文件URL时,请使用
NSURL
上的
fileURLWithPath:
,这将为给定路径创建正确的NSURL

-(IBAction)playVideo
{
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
NSLog(@"files array %@", filePathsArray);        
NSString *fullpath;
for ( NSString *apath in filePathsArray )
{
    fullpath = [documentsDirectory stringByAppendingPathComponent:apath];       
    vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSLog(@"vurl %@",vedioURL);
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[player.view setFrame: self.view.bounds];
[player.moviePlayer prepareToPlay];
[self.view addSubview:player.view];
player.moviePlayer.controlStyle = MPMovieControlStyleDefault;
player.moviePlayer.shouldAutoplay = YES;
[player.moviePlayer setFullscreen:YES animated:YES];
[player.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated: player];
}
别忘了添加MediaPlayer.framework和 #导入 在您各自的代码中。
祝你好运

每个人都会犯的最常见的错误是“使用一切”

NSURL *fileURL = [NSURL URLWithString:mVidPath];
                        ^^^^^^^^^^^^^
而不是

NSURL *fileURL = [NSURL fileURLWithPath:mVidPath];
                        ^^^^^^^^^^^^^^^

好吧,伙计们,这些答案当然值得大拇指和尊敬。谢谢你。@Dumoko……当然,我的朋友……活该……这就是我所做的你活该更像先生!