iosmpmovieplayercontrollerdocument-storage

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


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


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