的MPMoviePlayerController不会从Documents文件夹播放(MPMovieP

2019-08-01 05:52发布

Desperated。 大家好! 我有一些的MPMoviePlayerController问题。 我将它从一个NSBundle的视频工作。 但是,这不是我所需要的。 我需要从文件目录播放,因为那是我保存录制的视频的地方,至极的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

因此,该文件存在......我已经通过看问题:

保存在应用程序文件MPMoviePlayer加载和播放电影

的MPMoviePlayerController不与电影文件夹工作

MPMoviePlayerViewController玩影片从Documents目录- Objective-C的

我还检查UT带班参考,具体什么有关从文件播放。 我的项目设置:使用最新的iOS 6,部署目标5.0测试两个iOS6的iPhone模拟器和一个iPad与iOS 6,如果我忘了补充一点,请提醒我,我会马上去做。

请帮忙! :)

Answer 1:

那么找你不能建立文件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];

您应该添加目录和文件与路径stringByAppendingPathComponent的方法NSString ; 还创建文件URL使用时fileURLWithPath:NSURL ,这个预订购创建用于捐赠路径的正确NSURL。



Answer 2:

最常见的错误,每个人都做的是所有使用

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

代替

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


Answer 3:

-(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和#进口<MediaPlayer的/ MediaPlayer.h>在各自的代码。 祝好运!!!



文章来源: MPMoviePlayerController doesn't play from Documents folder