Class

MPMoviePlayerController

A movie player (of type MPMoviePlayerController) manages the playback of a movie from a file or a network stream. Playback occurs in a view owned by the movie player and takes place either fullscreen or inline. You can incorporate a movie player’s view into a view hierarchy owned by your app, or use an MPMoviePlayerViewController object to manage the presentation for you.

Overview

Movie players (iOS 4.3 and later) support wireless movie playback to AirPlay-enabled hardware such as Apple TV.. Starting in iOS 5.0, AirPlay playback is enabled by default. To disable AirPlay in your app, set the allowsAirPlay property to false. In iOS 8.0 and later, users access AirPlay compatible hardware through the Control Panel, no AirPlay control is displayed by the movie player.

When you add a movie player’s view to your app’s view hierarchy, be sure to size the frame correctly, as shown here:

MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds];  // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

Consider a movie player view to be an opaque structure. You can add your own custom subviews to layer content on top of the movie but you must never modify any of its existing subviews.

In addition to layering content on top of a movie, you can provide custom background content by adding subviews to the view in the backgroundView property. Custom subviews are supported in both inline and fullscreen playback modes but you must adjust the positions of your views when entering or exiting fullscreen mode. Use the MPMoviePlayerWillEnterFullscreen and MPMoviePlayerWillExitFullscreen notifications to detect changes to and from fullscreen mode.

This class supports programmatic control of movie playback, and user-based control via buttons supplied by the movie player. You can control most aspects of playback programmatically using the methods and properties of the MPMediaPlayback protocol, to which this class conforms. The methods and properties of that protocol let you start and stop playback, seek forward and backward through the movie’s content, and even change the playback rate. In addition, the controlStyle property of this class lets you display a set of standard system controls that allow the user to manipulate playback. You can also set the shouldAutoplay property for network-based content to start automatically.

You typically specify the movie you want to play when you create a new MPMoviePlayerController object. However, you can also change the currently playing movie by changing the value in the contentURL property. Changing this property lets you reuse the same movie player controller object in multiple places. For performance reasons you may want to play movies as local files. Do this by first downloading them to a local directory.

To facilitate the creation of video bookmarks or chapter links for a long movie, the MPMoviePlayerController class defines methods for generating thumbnail images at specific times within a movie. You can request a single thumbnail image using the thumbnailImageAtTime:timeOption: method or request multiple thumbnail images using the requestThumbnailImages(atTimes:timeOption:) method.

To play a network stream whose URL requires access credentials, first create an appropriate URLCredential object. Do this by calling, for example, the init(user:password:persistence:) method, as shown here:

NSURLCredential *credential = [[NSURLCredential alloc]
                        initWithUser: @"userName"
                            password: @"password"
                         persistence: NSURLCredentialPersistenceForSession];
 
self.credential = credential;
[credential release];

In addition, create an appropriate URLProtectionSpace object, as shown here. Make appropriate modifications for the realm you are accessing:

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                            initWithHost: "@streams.mydomain.com"
                                    port: 80
                                protocol: @"http"
                                   realm: @"mydomain.com"
                    authenticationMethod: NSURLAuthenticationMethodDefault];
 
self.protectionSpace = protectionSpace;
[protectionSpace release];

Add the URL credential and the protection space to the Singleton URLCredentialStorage object. Do this by calling, for example, the set(_:for:) method, as shown here:

[[NSURLCredentialStorage sharedCredentialStorage]
                    setDefaultCredential: credential
                      forProtectionSpace: protectionSpace];

With the credential and protection space information in place, you can then play the protected stream.

Movie Player Notifications

A movie player generates notifications to keep your app informed about the state of movie playback. In addition to being notified when playback finishes, your app can be notified in the following situations:

  • When the movie player begins playing, is paused, or begins seeking forward or backward

  • When AirPlay playback starts or ends

  • When the scaling mode of the movie changes

  • When the movie enters or exits fullscreen mode

  • When the load state for network-based movies changes

  • When meta-information about the movie itself becomes available

For more information, see the Notifications section in this document.

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:

  • H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps. (The Baseline profile does not support B frames.)

  • MPEG-4 Part 2 video (Simple Profile)

If you use this class to play audio files, it displays a white screen with a QuickTime logo while the audio plays. For audio files, this class supports AAC-LC audio at up to 48 kHz, and MP3 (MPEG-1 Audio Layer 3) up to 48 kHz, stereo audio.

Behavior in iOS 4.3 and Earlier

In iOS 4.3 and earlier, a new movie player was automatically prepared to play. Starting in iOS 5.0, in order to facilitate finer-grained playback control, a new movie player is not automatically prepared to play. See the discussion for the init(contentURL:) instance method.

Also, AirPlay was disabled by default in iOS 4.3 and earlier. Starting in iOS 5.0, the default for the allowsAirPlay property is true.

Behavior in iOS 3.1 and Earlier

In iOS 3.1 and earlier, this class implemented a full-screen movie player only. After creating the movie player and initializing it with a single movie file, you called the play method to present the movie. (The definition of the play method has since moved out of this class and into the MPMediaPlayback protocol.) The movie player object itself handled the actual presentation of the movie content.

Symbols

Creating and Initializing the Object

init!(contentURL: URL!)

Returns a MPMoviePlayerController object initialized with the movie at the specified URL.

Accessing Movie Properties

var contentURL: URL!

The URL that points to the movie file.

var movieSourceType: MPMovieSourceType

The playback type of the movie.

var movieMediaTypes: MPMovieMediaTypeMask

The types of media available in the movie.

var allowsAirPlay: Bool

Specifies whether the movie player allows AirPlay movie playback.

var isAirPlayVideoActive: Bool

Indicates whether the movie player is currently playing video via AirPlay.

var naturalSize: CGSize

The width and height of the movie frame.

var isFullscreen: Bool

A Boolean that indicates whether the movie player is in full-screen mode.

func setFullscreen(Bool, animated: Bool)

Causes the movie player to enter or exit full-screen mode.

var scalingMode: MPMovieScalingMode

The scaling mode to use when displaying the movie.

var controlStyle: MPMovieControlStyle

The style of the playback controls.

Accessing the Movie Duration

var duration: TimeInterval

The duration of the movie, measured in seconds.

var playableDuration: TimeInterval

The amount of currently playable content.

Accessing the View

var view: UIView!

The view containing the movie content and controls.

var backgroundView: UIView!

A customizable view that is displayed behind the movie content.

Controlling and Monitoring Playback

See also the methods of the MPMediaPlayback protocol.

var loadState: MPMovieLoadState

The network load state of the movie player.

var playbackState: MPMoviePlaybackState

The current playback state of the movie player.

var initialPlaybackTime: TimeInterval

The time, specified in seconds within the video timeline, when playback should start.

var endPlaybackTime: TimeInterval

The end time (measured in seconds) for playback of the movie.

var shouldAutoplay: Bool

A Boolean that indicates whether a movie should begin playback automatically.

var readyForDisplay: Bool

A Boolean that indicates whether the first video frame of the movie is ready to be displayed.

var repeatMode: MPMovieRepeatMode

Determines how the movie player repeats the playback of the movie.

var timedMetadata: [Any]!

Obtains the most recent time-based metadata provided by the streamed movie.

Playing a Pre-Roll Video Ad

Generating Thumbnail Images

func requestThumbnailImages(atTimes: [Any]!, timeOption: MPMovieTimeOption)

Captures one or more thumbnail images asynchronously from the current movie.

func cancelAllThumbnailImageRequests()

Cancels all pending asynchronous thumbnail image requests.

Retrieving Movie Logs

var accessLog: MPMovieAccessLog!

A snapshot of the network playback log for the movie player if it is playing a network stream.

var errorLog: MPMovieErrorLog!

A snapshot of the playback failure error log for the movie player if it is playing a network stream.

Constants

MPMovieLoadState

Constants describing the network load state of the movie player.

MPMovieControlStyle

Constants describing the style of the playback controls.

MPMovieFinishReason

Constants describing the reason that playback ended.

MPMoviePlaybackState

Constants describing the current playback state of the movie player.

MPMovieRepeatMode

Constants describing how the movie player repeats content at the end of playback.

MPMovieScalingMode

Constants describing how the movie content is scaled to fit the frame of its view.

MPMovieTimeOption

Constants describing which frame to use when generating thumbnail images.

MPMovieMediaTypeMask

Specifies the types of content available in the movie file.

MPMovieSourceType

Specifies the type of the movie file.

Thumbnail Notification User Info Keys

The following keys may be found in the userInfo dictionary of a MPMoviePlayerThumbnailImageRequestDidFinish notification.

Fullscreen Notification Keys

The following keys may be found in the userInfo dictionary of notifications for transitioning in or out of full-screen mode.

Playback Finished Notification Key

The following key may be found in the userInfo dictionary of a MPMoviePlayerPlaybackDidFinish notification.

Notifications

To be notified of changes in movie player state such as playability, or changes in the availability of movie information such as duration, register for the appropriate Notification. For all movie player notifications, the movie player whose state has changed is available as the object associated with the notification.

static let MPMovieDurationAvailable: NSNotification.Name

Posted when the duration of a movie has been determined. There is no userInfo dictionary.

static let MPMovieMediaTypesAvailable: NSNotification.Name

Posted when the available media types in a movie are determined. There is no userInfo dictionary.

static let MPMovieNaturalSizeAvailable: NSNotification.Name

Posted when the natural frame size of a movie is first determined or subsequently changes. There is no userInfo dictionary.

static let MPMoviePlayerDidEnterFullscreen: NSNotification.Name

Posted when a movie player has entered full-screen mode. There is no userInfo dictionary.

static let MPMoviePlayerDidExitFullscreen: NSNotification.Name

Posted when a movie player has exited full-screen mode. There is no userInfo dictionary.

static let MPMoviePlayerIsAirPlayVideoActiveDidChange: NSNotification.Name

Posted when a movie player has started or ended playing a movie via AirPlay. There is no userInfo dictionary.

static let MPMoviePlayerLoadStateDidChange: NSNotification.Name

Posted when a movie player’s network buffering state has changed. There is no userInfo dictionary.

static let MPMoviePlayerNowPlayingMovieDidChange: NSNotification.Name

Posted when the currently playing movie has changed. There is no userInfo dictionary.

static let MPMoviePlayerPlaybackDidFinish: NSNotification.Name

Posted when a movie has finished playing. The userInfo dictionary of this notification contains the MPMoviePlayerPlaybackDidFinishReasonUserInfoKey key, which indicates the reason that playback finished. This notification is also sent when playback fails because of an error.

static let MPMoviePlayerPlaybackStateDidChange: NSNotification.Name

Posted when a movie player’s playback state has changed. There is no userInfo dictionary.

static let MPMoviePlayerScalingModeDidChange: NSNotification.Name

Posted when the scaling mode of a movie player has changed. There is no userInfo dictionary.

static let MPMoviePlayerThumbnailImageRequestDidFinish: NSNotification.Name

Posted when a request to capture a thumbnail from a movie has finished whether the request succeeded or failed. Upon successful capture of a thumbnail, the userInfo dictionary contains values for the following keys:

static let MPMoviePlayerWillEnterFullscreen: NSNotification.Name

Posted when a movie player is about to enter full-screen mode. The userInfo dictionary contains keys whose values describe the transition animation used to enter full-screen mode. See Fullscreen Notification Keys.

static let MPMoviePlayerWillExitFullscreen: NSNotification.Name

Posted when a movie player is about to exit full-screen mode. The userInfo dictionary contains keys whose values describe the transition animation used to exit full-screen mode. See Fullscreen Notification Keys.

static let MPMovieSourceTypeAvailable: NSNotification.Name

Posted when the source type of a movie was previously unknown and is newly available. There is no userInfo dictionary.

static let MPMoviePlayerReadyForDisplayDidChange: NSNotification.Name

Posted when the ready for display state changes.

Instance Methods

Relationships

Inherits From