Search code examples
iosmpmovieplayercontroller

MPMoviePlayerController video not playing


I used MPMoviePlayerController in my application.
I got the right URL but MPMoviePlayer does not play video it shows only black screen.
My code is as below:

[video setImage:[UIImage imageNamed:@"video_active.png"] forState:UIControlStateNormal];
 NSString *filename=[NSString stringWithFormat:@"%@.mp4",[appdel.TempVideoUrl valueForKey:tag]];
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
 NSURL *movieURL = [NSURL fileURLWithPath:filePath] ;
 theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
 theMovie.view.frame = CGRectMake(106, 18, 216, 235);
 [uploadview addSubview:theMovie.view];

  // Play the movie.
 [theMovie pause];

I got the URL like this: file://localhost/var/mobile/Applications/6E1D4CB7-A08D-438B-9FE7-E2FD9B7B5EEC/Documents/136_1355227629.mp4


Solution

  • Add [theMovie prepareToPlay]; before calling [theMovie play];.

    prepareToPlay

    Prepares a movie player for playback. (required)

    - (void)prepareToPlay

    Discussion

    If a movie player is not already prepared to play when you call the play method, that method automatically calls this method. However, to minimize playback delay, call this method before you call play.

    Calling this method may interrupt the movie player’s audio session. For information on interruptions and how to resond to them, see Audio Session Programming Guide. Availability

    Available in iOS 3.2 and later.
    

    Declared In MPMediaPlayback.h

    Also you can use isPreparedToPlay for checking whether a movie player is ready to play.

    Please refer this link for more.