25

I use MPMoviePlayerController to play a local file in my Application Document folder which have I have downloaded for a server URL:

itemMoviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
 [self.view addSubview:itemMoviePlayerController.view];
 itemMoviePlayerController.fullscreen = YES;
 itemMoviePlayerController.movieSourceType = MPMovieSourceTypeFile;
 itemMoviePlayerController.initialPlaybackTime = -1.0;
 [itemMoviePlayerController play];

When I play .mov file just after I downloaded it, It shows up a black empty screen & app UI is unusable. But if play same local file next time, it plays fine. I even verified playState & localState for MPMoviePlayerController they seems fine. What could be reason for black empty screen?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ritika
  • 593
  • 1
  • 8
  • 21
  • 1
    Dec 2013 .. TIP - it is hugely flakey about **file name extensions** (files form the net). In my app, .MOV works, .mov crashes the iPhone hard. Bizarre. Also the simulator simply doesn't work, it's totally useless and just crashes. Finally on the MPMoviePlayerViewController approach works, MPMoviePlayerController is useless and broken. Finally you can not have more than one onscreen at the same time - they all just go black and fail. – Fattie Dec 01 '13 at 20:53
  • This looks exactly like my problem. – RainCast Feb 26 '16 at 03:37

13 Answers13

31

You need to retain your instance of MPMoviePlayerController i.e. as a property or an instance variable. The reference to the movie player is lost if you do not retain it.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
  • This was the solution for me. It doesn't matter if your project is ARC or not, you still need to retain the instance of MPMoviePlayerController, otherwise it is released and the movie does not work. – Pablo Jul 19 '13 at 22:49
  • Thanks this solved a bug I've been hunting for a long time as well in exactly the same context; showing a movie in a UITableViewCell. – simonfi Aug 05 '14 at 12:48
  • This is just.... perfect......!!! Wasted a couple hours till I found this! Nice answer!!! – Dean Leitersdorf Jun 09 '15 at 03:15
  • I have used MPMoviePlayerViewController and presented it but I am getting black screen while video loading – PK86 Aug 10 '15 at 08:39
  • I can't believe the answer is this simple. – An Chin Aug 11 '15 at 18:56
15

You could try to put [itemMoviePlayerController prepareToPlay]; before the [itemMoviePlayerController play];

The way preferred by Apple to display an only full screen video is to present a modal MPMoviePlayerViewController (as Hollance said). To present it, you should use something like :

 moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
 [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
 [itemMoviePlayerController play];

This is documented by Apple here

You can read there that you should keep a reference to your MPMoviePlayerViewController in order to dismiss it later with

[self dismissMoviePlayerViewControllerAnimated:moviePlayerViewController].

Neysor
  • 3,893
  • 11
  • 34
  • 66
Dulgan
  • 6,674
  • 3
  • 41
  • 46
13

I fixed this by putting

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer; 

in my .h file, and calling self.moviePlayer in all my code and it worked!

Luke
  • 11,426
  • 43
  • 60
  • 69
mattblessed
  • 772
  • 1
  • 12
  • 27
7

You need to use MPMoviePlayerViewController instead. Notice the word "View" in there.

Hollance
  • 2,958
  • 16
  • 15
  • I even tried using MPMoviePlayerViewController but that too causes same black empty screen – Ritika Dec 08 '11 at 12:51
  • This answer makes no sense at all. Why should the view-controller variant of the movie player be any different when it comes to pre buffering and identification of the content. – Till Mar 28 '12 at 14:30
  • It shouldn't. However, a common mistake is to use MPMoviePlayerController (without the "View"), which as of iOS 4 just gives you a blank screen (because its view isn't added anywhere). Because of this a lot of old examples no longer work. It did not turn out to be this person's problem, but "black empty screen" is often the result of this API change. – Hollance May 15 '12 at 13:49
  • Helped me. Seems like MPMoviePlayerViewController handles more errors and invokes MPMoviePlayerPlaybackDidFinishNotification when anything bad happens. This way you can dismiss movie player when something bad happens more easily. – Lukasz Jun 12 '12 at 08:27
3

I hope this will help. I solved this problem in my project

 -(void)startPlayingMovie
{
    NSLog(@"startPlayingMovie");
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"Start_video" ofType:@"mov"]];
    moviePlayer =  [[MPMoviePlayerViewController alloc]
                    initWithContentURL:url];
    moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer.moviePlayer];

moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.moviePlayer.shouldAutoplay = YES;
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer setFullscreen:YES animated:NO];

NSLog(@"endPlayingMovie");

}
  • `moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);` => `moviePlayer.view.frame = self.view.bounds;` – kelin May 12 '15 at 13:26
  • it's not, because height and width are reversed – manecosta Dec 03 '15 at 20:55
2

Same thing happened to me. It turns out, I had tried to play the movie while the UIImagePicker wasn't dismissed yet.

In my UIImagePickerDelegate I had to first dismiss the UIImagePicker Popup and then open the ViewController managing the MPMediaPlayerController:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)infoDict
{
    //the first thing to do: dismiss the media picker
    if ([ipPop isPopoverVisible])
    {
        [ipPop dismissPopoverAnimated:ANIMATION_SETTING];
    }

    myMediaPlayerViewController * playerVC = [[myMediaPlayerViewController alloc] initWithMediaDict:infoDict];
    [self.navigationController pushViewController:playerVC animated:ANIMATION_SETTING];
}
Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
cayeric
  • 185
  • 1
  • 5
2

I had this same problem and it made me crazy. It would work on one view controller fine (audio and video), but not work on another (black screen with just audio). In the end, all I did was CHANGE THE ORDER of the calls: I simply waited until I was done configuring the movie player before adding it to the view. In other words, I called "addSubview" on the line just before the call to "play".

ghostatron
  • 2,620
  • 23
  • 27
1

This is the code I'm using to play a file from URL:

MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:contentUrl]];
movieViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:movieViewController];
[movieViewController release];

It seems to work fine for me. Two notes:

  • Some simulators (like the current iOS 5.0) crash when playing a movie, but it works on a real device
  • If you leave out the movieSourceType part, a black screen is shown for about a second before the movie starts
talkol
  • 12,564
  • 11
  • 54
  • 64
0

I had the same problem, solved it changing the extension of the file being played from .mpg to .mp4. apparently MPMoviePlayerController expects a correct extension, though from the documentation it is not clear to me that this is a requirement:

http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

Supported Formats This class plays any movie or audio file supported in iOS. This includes both streamed content and fixed-length files. For movie files, this typically means files with the extensions .mov, .mp4, .mpv, and .3gp and using one of the following compression standards:

0

This issue seems fixed after updating device iOS to 5.0.
Seems to iOS SDK issue for previous version

Ritika
  • 593
  • 1
  • 8
  • 21
0

you must do like that with CGRectMake(0, 0, 0, 0) in the finished callback!

-(void)playMovieAtURL:(NSURL*)theURL

{
    MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
    theMovie.scalingMode=MPMovieScalingModeAspectFill;

    theMovie.view.frame = CGRectMake(2, 246, 317, 70);
    [self.view addSubview:theMovie.view];

    // Register for the playback finished notification. 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(myMovieFinishedCallback:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:theMovie];

    // Movie playback is asynchronous, so this method returns immediately. 
    [theMovie play];
} 

// When the movie is done,release the controller. 
-(void)myMovieFinishedCallback:(NSNotification*)aNotification 
{
    MPMoviePlayerController* theMovie=[aNotification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:theMovie];

    theMovie.view.frame = CGRectMake(0, 0, 0, 0);
    // Release the movie instance created in playMovieAtURL
    [theMovie release];

}
plop
  • 11
0

I was having a similar issue. After playback there comes an empty screen, which prevents me to play video again. Well here is my work around. That way play button doesn't disappear.

[self.movieController stop]; 
[self.movieController prepareToPlay];
[self.movieController pause];
0

Also, If you are using http URLs, make sure you disable App Transport Security in your projects *.plist file. It will also cause a black screen.

Add the following code:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>

Here:

   [INSERT CODE ABOVE HERE]
  </dict>
</plist>
----- BOTTOM OF PLIST FILE
Fostah
  • 2,947
  • 4
  • 56
  • 78