Playing video with Apple TV integration

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.

Getting ready

Following on from our previous recipe, create a new Single View Application, and name it PlayVideoExample.

How to do it...

To begin, follow these simple steps as outlined in the following order:

  1. Add a reference to the MediaPlayer.framework to your project.
  2. Select the ViewController.xib files from the Project Navigator window.
  3. From the Object Library, drag a UIButton object, and add this to our view.
  4. From the Attributes Inspector section, change the Title property to Play Video.
  5. Create an outlet for this UIButton, and name it btnPlayVideo.
  6. Create the action method for the Play Video button and name it btnPlayVideo.
  7. Our next step is to create the code functionality that will be responsible for playing the video when the Play Video button is clicked.
  8. Open the ViewController.h interface file from the Project Navigator window.
  9. Next, create the following code sections as highlighted in the code snippet:
    //  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;
    
  10. Open the ViewController.m implementation file from the Project Navigator window, and enter in the following highlighted code sections:
    @synthesize btnPlayVideo;
    @synthesize moviePlayerController;
    
  11. Next, modify the 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];
    }
  12. Next, create the 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];
    }
  13. Then, Build and Run the application by choosing Product | Run from the Product menu, or alternatively pressing Command + R.

When the compilation completes, the following screenshot shows what the icon looks like when AirPlay has been enabled:

How to do it...

How it works...

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.

Note

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

See also

  • The Playing music using the MediaPlayer framework recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset