Contents

MPMoviePlayerController

A type of movie player that manages the playback of a movie from a file or a network stream.

Declaration

class MPMoviePlayerController

Overview

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.

Movie players support wireless movie playback to AirPlay-enabled hardware such as Apple TV. 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 MPMoviePlayerWillEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification 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 thumbnailImage(atTime: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.

Topics

Creating and initializing the object

Accessing movie properties

Accessing the movie duration

Accessing the view

Controlling and monitoring playback

Generating thumbnail images

Retrieving movie logs

Constants

See Also

Deprecated symbols