Playing a Downloaded Video from the Documents Directory with Cocoa-Touch

倾然丶 夕夏残阳落幕 提交于 2019-11-30 17:05:26

问题


I'm trying to get my app to play a video file that's been downloaded to the documents directory. I know the file is getting downloaded, but I can't seem to get the file to play, here is my code:

-(IBAction)play{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

NSURL *movieURL = [NSURL fileURLWithPath:path];


_player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:_player.view];

_player.controlStyle = MPMovieControlStyleDefault;
_player.shouldAutoplay = YES;


[_player setFullscreen:YES animated:YES];


[_player play];

}

回答1:


This looks like some kind of bug, but you have to set your path like that:

 NSString *vidPath = [[NSBundle mainBundle] pathForResource:@"promo" ofType:@"mp4"];
 NSURL *url = [NSURL fileURLWithPath:vidPath isDirectory:NO]; //THIS IS THE KEY TO GET THIS RUN :) 
 [introPlayer setContentURL:url];



回答2:


Issue will be with this line: NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

Change that to NSString *path = [documentsDirectory stringByAppendingPathComponent:@"piggy.m4v"];



来源:https://stackoverflow.com/questions/11549143/playing-a-downloaded-video-from-the-documents-directory-with-cocoa-touch

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