This is rather strange, but it seems to be okay if you make your MPMoviePlayerController as a property instead of a local variable. Something seems to be happening backstage. I think this is related to ARC. Do you use ARC?
This is also a problem with adding your path:
// You've already got the full path to the documents directory here. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; // Now you're appending the full path to the documents directory to your bundle path NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
When I run your code in a simulator, the path is as follows:
/ Users / mlong / Library / Application Support / iPhone Simulator / 5.1 / Applications / 8CFB9B94-BD6A-442C-A525-573FE343506D / VidoePlayer.app / Users / mlong / Library / Application Support / iPhone Simulator / 5.1 / Applications / 8CFB9B94-BD6A -442C-A525-573FE343506D / Documents / one.mp4
It should be simple:
/ Users / mlong / Library / Application Support / iPhone Simulator / 5.1 / Applications / 8CFB9B94-BD6A-442C-A525-573FE343506D / Documents / one.mp4
So just delete this line:
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
And then change your player instance to this:
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]]; [[_moviePlayer view] setFrame:[[self view] bounds]]; [[self view] addSubview: [_moviePlayer view]]; _moviePlayer.scalingMode = MPMovieScalingModeAspectFit; [_moviePlayer play];
So, you should add MPMoviePlayerController as a property of your view controller.
Matt long
source share