Video Playback from within an iOS 5 iPad Application

Revision as of 20:14, 27 October 2016 by Neil (Talk | contribs) (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Revision as of 20:14, 27 October 2016 by Neil (Talk | contribs) (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
PreviousTable of ContentsNext
An Example iOS 5 iPad Camera ApplicationPlaying Audio on an iPad using AVAudioPlayer


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


Whilst the iPhone has always supported video playback, the appeal of watching on the smaller screen has always been less than ideal, especially for prolonged viewing periods. With its larger screen, the iPad is without question an excellent platform for video playback. Video playback support on the iPad, as with the iPhone, is provided by the iOS 5 MPMoviePlayerController class.

This chapter presents an overview of the MPMoviePlayerController class followed by a step by step example of the use of this class to play a movie within an iOS 5 iPad application.

An Overview of the MPMoviePlayerController Class

The sole purpose of the MPMoviePlayerController is to play video content. It is initialized with the URL of the media to be played (either a path to a local movie file on the device or the URL of network based media). The movie player view is added as a subview of the current view, configured using a variety of properties and then displayed to the user. The playback window can run in a full screen mode, or embedded in an existing view.

By default, the movie player includes a number of controls that enable the user to manage the playback experience. The exact controls displayed to the user may be configured by setting the movie player’s controlStyle property. Playback may also be controlled from the application code by implementing the methods defined in the MPMediaPlayback protocol.

The MPMoviePlayerController class also employs the target-action model for notifying the application of events such as the movie starting, finishing, being paused, entering and leaving full screen mode etc.

Supported Video Formats

The MPMoviePlayerController class supports the playback of movies of type .mov, .mp4, .mpv and .3gp. In terms of compression, the class supports H.264 Baseline Profile Level 3.0 and MPEG-4 Part 2 video.


The iPad Movie Player Example Application

The objective of the remainder of this chapter is to create a simple application that will play back a video when a button is pressed. The video will be streamed over the internet from a movie file located on a web server.

Begin by launching Xcode and creating a new iOS iPad application project based on the Single View-based application template, naming the product and class prefix movie when prompted to do so.

Adding the MediaPlayer Framework to the Project

The MPMoviePlayerController class is contained within the MediaPlayer framework. This framework must, therefore, be included in any projects that make use of this class. This can be achieved by selecting the product target entry from the project navigator panel (the top item named movie) and clicking on the Build Phases tab in the main panel. In the Link Binary with Libraries section click on the ‘+’ button, select the MediaPlayer.framework entry from the resulting panel and click on the Add button.

Declaring the Action Method and MoviePlayer Instance

The application user interface is going to consist of a single button which, when pressed, will initiate the movie playback process. We therefore need to declare an action method called playMovie in our movieViewController.h file. It is also imperative that the <MediaPlayer/MediaPlayer.h> file be imported to avoid a catalog of unresolved references when the application is compiled and also add a reference to an instance of the MPMoviePlayerController class:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface movieViewController : UIViewController

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
-(IBAction) playMovie;
@end

Select the movieViewController.m and add the @synthesize directive for the moviePlayer:

#import "movieViewController.h"

@interface movieViewController ()

@end

@implementation movieViewController
@synthesize moviePlayer;
.
.
@end

Designing the User Interface

Select the movieViewController.xib file and display the Object library (View -> Utilities -> Object Library). Drag a single UIButton instance to the view window and change the text on the button to “Play Movie”. With the button selected display the connections inspector window (View -> Utilities -> Show Connections Inspector), click on the circle to the right of the Touch Up Inside event and drag the blue line to the File’s Owner object. From the resulting menu, select the playMovie action method.

Implementing the Action Method

The next step is to write the code for the action method so that video playback is initiated when the button is touched by the user. The code for the method should be placed in the movieViewController.m file as follows:

-(void)playMovie
{
   NSURL *url = [NSURL URLWithString:
      @"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];

    moviePlayer =  [[MPMoviePlayerController alloc]
                 initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self
                   selector:@selector(moviePlayBackDidFinish:)
                   name:MPMoviePlayerPlaybackDidFinishNotification
                   object:moviePlayer];

    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];
}

The above method constructs an NSURL object using the URL of a web based video file. This is then used in the creation of a new instance of the MPMoviePlayerController class. A notification is then configured such that the moviePlaybackDidFinish method is called when the playback finishes. Next, properties are set to ensure that the standard movie controls are available to the user and that the movie automatically starts playing once it is ready. Finally the movie player object is added as a subview to the current view and displayed to the user in full screen mode.

The Target-Action Notification Method

The playButton action method declared that when the movie has finished playing the movie player object is to call a method named moviePlaybackDidFinish. It is the responsibility of this method to cancel the notification and remove the movie player interface from display. Edit the movieViewController.m file and add this method as follows:

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] 
      removeObserver:self
      name:MPMoviePlayerPlaybackDidFinishNotification
      object:player];

    if ([player
        respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [player.view removeFromSuperview];
    }
}

Build and Run the Application

<google>ADSDAQBOX_FLOW</google> With the design and coding phases complete, all that remains is to build and run the application. Click on the Run button located in the toolbar of the main Xcode project window. Assuming that no errors occur, the application should launch within the iOS Simulator or device. Note that in addition to the application code, the movie.mov file added as a resource to the project also needs to be loaded. If a large video file was used it may take some time before the application starts up. Once loaded, touching the Play Movie button should launch the movie player in full screen mode and playback should automatically begin:


Video playback on in iPad iOS 5 app

Figure 58-1


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of ContentsNext
An Example iOS 5 iPad Camera ApplicationPlaying Audio on an iPad using AVAudioPlayer