Video Playback from within an iOS 6 iPhone Application
Previous | Table of Contents | Next |
An Example iOS 6 iPhone Camera Application | Playing Audio on an iPhone using AVAudioPlayer (iOS 6) |
<google>BUY_IOS6</google>
Whilst the iPhone 3GS model introduced support for recording videos using the built in camera, all iPhone models and iOS versions have included support for video playback. Video playback support in iOS 6 is provided by the 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 6 iPhone 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 iPhone 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 iPhone application project based on the Single View Application template, naming the product and class prefix Movie when prompted to do so, making sure that the Use Storyboards and Use Automatic Reference Counting options are enabled.
Adding the MediaPlayer Framework to the Project
<google>ADSDAQBOX_FLOW</google> 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.
Designing the User Interface
Select the MainStoryboard.storyboard file and display the Object library (View -> Utilities -> Object Library). Drag a single Button instance to the view window and change the text on the button to “Play Movie”.
Display the Assistant Editor, Ctrl-click on the button object and drag the line to the area immediately beneath the @interface line in the Assistant Editor panel. Release the line and, within the resulting connection dialog, establish an Action method on the Touch Up Inside event configured to call a method named playMovie.
Declaring the MoviePlayer Instance
Before proceeding, it is imperative that the <MediaPlayer/MediaPlayer.h> file be imported to avoid a catalog of unresolved references when the application is compiled. A reference to an instance of the MPMoviePlayerController class is also going to required. Select the MovieViewController.h file, therefore, and modify it as follows:
#import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface MovieViewController : UIViewController @property (strong, nonatomic) MPMoviePlayerController *moviePlayer; - (IBAction)playMovie:(id)sender; @end
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 stub method for the playMovie action has been created in MovieViewController.m and should be modified as follows.
-(void)playMovie:(id)sender { 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
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:
Figure 67-1
<google>BUY_IOS6</google>
Previous | Table of Contents | Next |
An Example iOS 6 iPhone Camera Application | Playing Audio on an iPhone using AVAudioPlayer (iOS 6) |