Search code examples
iosobjective-ciphonempmovieplayercontroller

Can't play movie in MPMovieplayercontroller


I can't play a movie in MPMovieplayercontroller. I have searched a lots on google but every time I got a black screen. Also I tried to check isPreparedToPlay property but it is always false.

self.moviePlayer = [[MPMoviePlayerController alloc] init];

[self.moviePlayer setContentURL:[self.videos objectAtIndex:0]];
NSLog(@"Playing videos : %@", [self.moviePlayer contentURL]);

 NSLog(@"Playing videos : %@", [[self.moviePlayer contentURL] absoluteString]);

[self.moviePlayer.view setFrame:CGRectMake (0, 0, 320, 460)];
[self.view addSubview:self.moviePlayer.view];
//self.moviePlayer.movieSourceType    = MPMovieSourceTypeFile;
/*[self.moviePlayer.view setFrame:self.view.bounds];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:NO];*/  
[self.moviePlayer play];

First NSLog print something weird (why there is "..." ?):

/Users/Athos/Library/Developer/CoreSimulator/Devices/15855707-E728-4542-8DAA-3B71362D7DB6/data/Containers/Data/Applicati ... unny.mp4

But second one is good (i check manually if the file was there:

/Users/Athos/Library/Developer/CoreSimulator/Devices/15855707-E728-4542-8DAA-3B71362D7DB6/data/Containers/Data/Application/2ACD0002-1A1E-484C-B4E4-9486EBCFF74A/Documents/BuckBunny.mp4

Do you have any clue ? Thanks in advance


Solution

  • Try this one :

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/BuckBunny.mp4"];    
    NSURL *movieURL = [NSURL fileURLWithPath:path];
    
    self.moviePlayer = [[MPMoviePlayerController alloc] init];
    
    if(movieURL!=nil){
        [self.moviePlayer setContentURL:movieURL];
        [self.moviePlayer.view setFrame:CGRectMake (0, 0, 320, 460)];
        [self.view addSubview:self.moviePlayer.view];
        [self.moviePlayer play];
    }