I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [moviePlayer view]];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer play];
All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.
First of all, Where is the video stored?
If your video is in the documents directory, don't append the documents directory path to the bundle path.
Just try with the
filePath
variable:However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in
jinx
response.It took me few mins to debug the problem but the answer is quite simple. Here is the deal:
If you want to MPMoviePlayerViewController to play from a web URL use this: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];
And if you want MPMoviePlayerViewController to play it from App Bundle use this: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];
The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:
Try asking your bundle directly and not setting up the file path manually
Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.
Supported video formats by MPMoviePlayerController class are following
I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article
hope Help it