i play a video stored in a subfolder in ios6 documents using MPMoviePlayerController ,but it doesn't work

StackOverflow https://stackoverflow.com/questions/22740642

Question

i play a video stored in a subfolder named test1 in ios6 documents using MPMoviePlayerController, but it doesn't work

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];

well :url is /Users/fengsu/Library/Application Support/iPhone Simulator/6.0/Applications/53C97346-C8FD-42DF-AD37-F2B14D5D3984/Documents/test1/v1.mp4

I think there is something wrong with the url,but i don't konw why,please help me ,thank you very much

Was it helpful?

Solution

The problem is how you construct the NSURL object.

MPMoviePlayerController expects URLs pointing to local files to have a file:// prefix.

The way to do this is as follows:

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...

OTHER TIPS

Use [NSURL fileURLWithPath:moviePath] That is used to open urls from files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top