Search code examples
iosmpmovieplayercontroller

MPMoviePlayerController video not playing from local document in real phone


When I use the MPMoviePlayerController in my app, it does not play videos and just shows a black rectangle.

my code is as follows:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = nil;
//filePath =[NSString stringWithFormat:@"%@/%@.mp4", documentsDirectory, fileName];
filePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",fileName]];
NSLog(@"%@",filePath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists){
    NSLog(@"exist!");
}else{
    NSLog(@"no exist!");
}
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];


[moviePlayer.view setFrame:self.view.frame];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
moviePlayer.fullscreen = YES;
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];


-(void)playMovie:(NSNotification *)notification {
 MPMoviePlayerController *player = notification.object;
 if (player.loadState & MPMovieLoadStatePlayable)
{
    NSLog(@"Movie is Ready to Play");
    [player play];
}

}

The output is:

 iBeaconCollection[10320:60b] /var/mobile/Applications/107F6DE4-AE31-40E0-BAA0-  76A956B72116/Documents/101A ~ Hallway to 101-103.mp4

 2014-06-11 14:13:27.302 iBeaconCollection[10320:60b] exist!

Hope someone could help me! Thanks.


Solution

  • Use MPMoviePlayerViewController, and then try and make sure that the file is in fact an MP4, as you are appending that extension.