The AirPlay framework is an updated framework that lets you stream audio and video content from any iOS device out to any Airplay-enabled device that is capable of playing audio and video such as television sets and audio systems.
Starting with iOS 5, developers now have the flexibility to incorporate Airplay content into their applications, and have this information presented out to a nearby Apple TV receiver. In this section, we will take a look at how to create a simple application to playback video content on an iOS device, and then take a look at the steps involved to output this to an Apple TV device.
Following on from our previous recipe, create a new Single View Application, and name it PlayVideoExample.
To begin, follow these simple steps as outlined in the following order:
MediaPlayer.framework
to your project.ViewController.xib
files from the Project Navigator window.UIButton
object, and add this to our view.UIButton
, and name it btnPlayVideo
.btnPlayVideo
.ViewController.h
interface file from the Project Navigator window.// ViewController.h // PlayVideoExample // Created by Steven F Daniel on 20/11/12. // Copyright (c) 2012 GenieSoft Studios. All rights reserved. #import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController : UIViewController // Declare the Getters and Setters for each of our objects. @property (strong, nonatomic) IBOutlet UIButton *btnPlayVideo; @property (strong, nonatomic) MPMoviePlayerController *moviePlayerController;
ViewController.m
implementation file from the Project Navigator window, and enter in the following highlighted code sections:@synthesize btnPlayVideo; @synthesize moviePlayerController;
btnPlayVideo:
method as shown in the code snippet:#pragma mark Handle Playback of the video when button is pressed. - (IBAction)btnPlayVideo:(id)sender { // Initialize our moviePlayer Controller with the video path NSString *moviePath = [[NSBundle mainBundle]pathForResource:@"GenieCompanyVideo" ofType:@"mp4"]; NSURL *movieURL = [NSURL fileURLWithPath:moviePath isDirectory:NO]; self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; // Set up our notifications to determine when movie completes [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController]; // Add the movie player controller to the view and // determine if AirPlay is available [self.view addSubview:self.moviePlayerController.view]; if ([self.moviePlayerController respondsToSelector:@selector(setAllowsAirPlay:)]){ [self.moviePlayerController setAllowsAirPlay:YES]; } // Initialize the movie player properties and play the video. self.moviePlayerController.fullscreen = YES; self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFit; [self.moviePlayerController play]; }
moviePlaybackComplete:
as method shown in the code snippet: #pragma mark Handle once the video has finished playback. - (void)moviePlaybackComplete:(NSNotification *)notification { self.moviePlayerController = [notification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController]; [self.moviePlayerController.view removeFromSuperview]; }
When the compilation completes, the following screenshot shows what the icon looks like when AirPlay has been enabled:
In this recipe, we declared a variable (NSString
) moviePath
that will contain the file path to our movie file and then create a (NSURL
) movieURL
that converts our file path to an object that is needed by the MPMoviePlayerController
method when it is being initialized.
Next, we set up a notification method to determine when the video playback completes by using a notification method called MPMoviePlayerPlaybackDidFinishNotification
to the NSNotificationCenter
property which the calls the
moviePlaybackComplete:
method and then add the MPMoviePlayerController
view to our custom view controller, so that it will appear on the screen and then use the
respondsToSelector:
method of the MPMoviePlayerController
object to cater for older iOS devices that don't support the allowsAirPlay
property.
In order to provide AirPlay functionality
we enable a special property on our MPMoviePlayerController
object, by setting the allowsAirPlay
property to YES
and then specify that we want to display our video in full screen.
We then tell the moviePlayerController
method to commence playback and then modify the scalingMode
property of the MPMoviePlayerController
object. By setting this property, it will determine how the movie image adapts to fill the playback size that you have defined. The following scaling modes currently exist, and are displayed here:
MPMovieScalingModeNone
MPMovieScalingModeAspectFit
MPMovieScalingModeAspectFill
MPMovieScalingModeFill
Finally, we create the moviePlaybackComplete:
method and retrieve the object using the [notificationobject]
statement and then reference it with the new moviePlayerController
pointer. We then send a message back to the NSNotificationCenter
method
that removes the observer we previously registered within our playVideo
method. We finally proceed with cleaning up our custom view controller from our display.
For more information on the MPMoviePlayerController
class, you can refer to the Apple Developer Documentation at the following location: https://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/occ/cl/MPMoviePlayerController