Wednesday, April 6, 2011

MPMoviePlayer Tips

Trying to make MPMoviePlayer do "things" it doesn't really want to. Finding lots of tips, saving here as reference. Yes, it was a long day. Too many tips to be remembered tomorrow.

First Rule: "There can be only one". You cannot use two MPMoviePlayers at the same time. Apple (SDK) makes some things really easy - and then there are these other things.

prepareToPlay (iOS 3.2+)Prepares the current item for playback. (required)
- (void)prepareToPlay
This method is called automatically when you call the play method. Calling it before you call play gives the receiver a chance to prepare items sooner and may result in decreased latency when starting playback. However, calling this method may also interrupt any active audio sessions.

movieSourceType (iOS 3.2+)
The playback type of the movie.
@property (nonatomic) MPMovieSourceType movieSourceType
The default value of this property is MPMovieSourceTypeUnknown. This property provides a clue to the playback system as to how it should download and buffer the movie content. If you know the source type of the movie, setting the value of this property before playback begins can improve the load times for the movie content. If you do not set the source type explicitly before playback, the movie player controller must gather this information, which might delay playback.

If your video URL doesn't have filename extension and won't play from network, but does play when saved as file and played locally, check server sends you correct MIME type (received via HTTP Content-Type header). Please note that it has to be "correct MIME type" according to iOS, not by you or server side developers. Yes, there is a difference. Really.

Correct MIME types for HTTP Live Streaming are:
.M3U8 application/x-mpegURL or
.ts   video/MP2T

If you see a white flash, when starting movie play, try setting all possible backgrounds to black:
moviePlayerViewController.view.backgroundColor = [UIColor blackColor];

Do you have strange problems playing video (iPad running  iOS 3.2 or 3.2.1)? Does device reboot solve the problem for a while? Can you  play movie either in UIWebView or MPMoviePlayer, but not both? Seems like AVAudioPlayer and MPMoviePlayerController cannot be used at the same time e.g. by some other application at background (iPod). Try setting useApplicationAudioSession to NO:

useApplicationAudioSession (iOS 3.2+)
A Boolean value that indicates whether the movie player should use the application’s audio session.
@property(nonatomic) BOOL useApplicationAudioSession
The default value of this property is YES. Setting this property to NO causes the movie player to use a system-supplied audio session with a nonmixable playback category.

You should use presentMoviePlayerViewControllerAnimated and dismissMoviePlayerViewControllerAnimated to show and hide moviePlayer. That's why they exist, designed to do their stuff in a nice and smooth way.

You cannot change "Loading Movie..." text in MPMoviePlayerController. You can try to cover it with a custom UIImageView added as a subView to your MoviePlayerControllerView.

You cannot stop "This movie could not be played" error notes, there just isn't any API to control or (non-private) method to overwrite. The good news is that everybody has the same problem.

When trying to stop movie before it has reached end, try setting initialPlaybackTime to -1. Might also pause movie before stopping it. Bit of magic, but might help sometimes...

initialPlaybackTime (iOS 3.0+)
The time, specified in seconds within the video timeline, when playback should start.
@property (nonatomic) NSTimeInterval initialPlaybackTime
For progressively downloaded content, playback starts at the closest key frame prior to the provided time. For video-on-demand content, playback starts at the nearest segment boundary to the provided time. For live video streams, the playback start time is measured from the start of the current playlist and is rounded to the nearest segment boundary. The default value of this property is -1, which indicates the natural start time of the movie.

Random links:


  1. Do you already have some problem when you are playing an audio streaming file try to play the next audio streaming file while over 3g network and your app is in background?

  2. Don't know, but guessing end of audio stream at background would also break the background mode. iOS does NOT offer real multitasking service to 3rd party developers.