MPMoviePlayerController doesn't play from Documents folder

怎甘沉沦 提交于 2019-12-03 16:21:47

Well you'r not building the file URL the correct way, you should do it like this:

NSString *docPath = [NSSearchPathForDirectoriesInDomains
                     (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:@"/Oct_08_2012_10_00_51.mp4"];
NSURL *docUrl= [NSURL fileURLWithPath:docaPathFull];

You should add directories and file to a path with the stringByAppendingPathComponent method of NSString; Also when creating the file URL use fileURLWithPath: on NSURL, this willl create a correct NSURL for the giving path.

The most common mistake that everyone do is all use

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

instead of

NSURL *fileURL = [NSURL fileURLWithPath:mVidPath];
                        ^^^^^^^^^^^^^^^
-(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];
}

Don't forget to add MediaPlayer.framework and #import < MediaPlayer/MediaPlayer.h> in your respective code. Good Luck!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!