Play a Video Using MPMoviePlayerController

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-03

This recipe shows how to play a video using an MPMoviePlayerController.

ℹ️

Note: The MPMoviePlayerController class has been deprecated in iOS 9 and later. Use AVPictureInPictureController or AVPlayerViewController instead. Please see our Picture in Picture documentation.

Recipe

  1. Add a class variable for an MPMoviePlayerController.
MPMoviePlayerController moviePlayer;
  1. Add code to play a movie. The following code does this in a UIButton’s TouchUpInside event handler:
playMovie.TouchUpInside += delegate {
    moviePlayer = new MPMoviePlayerController (NSUrl.FromFilename ("sample.m4v"));

    View.AddSubview (moviePlayer.View);
    moviePlayer.SetFullscreen (true, true);
    moviePlayer.Play ();
} ;
  1. Ensure that any video files you intend to use are set with Build Action Content. You can do this by right-clicking on the video files and selecting **Build Action** in the context menu that appears.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.