Playing Video on the iPhone and iPad

Playing Video on the iPhone and iPad

Playing videos is one of the most common tasks on the iPhone. On the iPhone, all videos must be played full-screen. However, on the iPad, this rule has been relaxed — you can now embed videos within your iPad applications. This makes it possible for you to embed more than one video in any View window. This article discusses the two classes supported in the iPhone SDK for video playback.

Playing Video on the iPhone

Using Xcode, create a new View-based Application (iPhone) project and name it PlayVideo. Drag a sample video into the Resources folder of your Xcode project (see Figure 1).

Figure 1. Adding a video to the Resources folder

To playback video on your iPhone application, you need to add the MediaPlayer framework to your project. Right-click on Frameworks folder and add the MediaPlayer.framework to your project (see Figure 2).

Figure 2. Adding the MediaPlayer framework

In the PlayVideoViewController.m file, code the following in bold:

#import "PlayVideoViewController.h"#import @implementation PlayVideoViewController- (void)viewDidLoad {        NSString *url = [[NSBundle mainBundle]         pathForResource:@"Stock_Footage_Demobroadband"                  ofType:@"mp4"];    MPMoviePlayerController *player =         [[MPMoviePlayerController alloc]             initWithContentURL:[NSURL fileURLWithPath:url]];        [[NSNotificationCenter defaultCenter]         addObserver:self           selector:@selector(movieFinishedCallback:)                                                                name:MPMoviePlayerPlaybackDidFinishNotification             object:player];        //---play movie---    [player play];        [super viewDidLoad];    }- (void) movieFinishedCallback:(NSNotification*) aNotification {    MPMoviePlayerController *player = [aNotification object];    [[NSNotificationCenter defaultCenter]         removeObserver:self                  name:MPMoviePlayerPlaybackDidFinishNotification                object:player];        [player autorelease];    }

Basically, you use the MPMoviePlayerController class to control the playback of a video:

MPMoviePlayerController *player =         [[MPMoviePlayerController alloc]             initWithContentURL:[NSURL fileURLWithPath:url]];

You then use the NSNotificationCenter class to register a notification so that when the movie is done playing (either the movie ends or the user taps on the Done button located on the top left corner of the screen):

     [[NSNotificationCenter defaultCenter]         addObserver:self           selector:@selector(movieFinishedCallback:)                                                                name:MPMoviePlayerPlaybackDidFinishNotification             object:player];

When the movie stops playing, you should unregister the notification and then release the player object:

- (void) movieFinishedCallback:(NSNotification*) aNotification {    MPMoviePlayerController *player = [aNotification object];    [[NSNotificationCenter defaultCenter]         removeObserver:self                  name:MPMoviePlayerPlaybackDidFinishNotification                object:player];        [player autorelease];    }

To test the application on the iPhone Simulator, press Command-R. Figure 3 shows the movie playing on the iPhone Simulator in full screen.

Figure 3. Playing the movie in landscape mode.

In fact, all movies played full-screen on the iPhone. Observe that by default, the movie is always played in landscape mode. To force the movie to be played in portrait mode, you can set the orientation of the player, like this:

[player setOrientation:UIDeviceOrientationPortrait animated:NO]; 

Doing so forces the player to play in portrait mode (see Figure 4).

Figure 4. Playing the movie in portrait mode.

Playing Video on the iPad

Playing video on the iPad is similar to that on the iPhone. However, you need to make some modifications to the code, if not the video will not play back correctly.

If you now convert the application to run natively on the iPad (see my previous article on how to port your iPhone apps to iPad) and test it on the iPad Simulator, you will realize that the video plays with sound, but nothing shows.

To play the movie correctly on the iPad, modify the application as shown below:

- (void)viewDidLoad {        NSString *url = [[NSBundle mainBundle]         pathForResource:@"Stock_Footage_Demobroadband"                  ofType:@"mp4"];    MPMoviePlayerController *player =         [[MPMoviePlayerController alloc]             initWithContentURL:[NSURL fileURLWithPath:url]];        [[NSNotificationCenter defaultCenter]         addObserver:self           selector:@selector(movieFinishedCallback:)               name:MPMoviePlayerPlaybackDidFinishNotification             object:player];        //---play partial screen---    player.view.frame = CGRectMake(184, 200, 400, 300);    [self.view addSubview:player.view];           //---play movie---    [player play];        [super viewDidLoad];    }

Essentially, you are now specifying the area to play back the movie. On the iPad, you can now embed the movie by adding the view exposed by the player to your view window:

    player.view.frame = CGRectMake(184, 200, 400, 300);    [self.view addSubview:player.view]; 

When you now press Command-R to test the application on the iPad Simulator again, you will see the movie displayed within the View window (see Figure 5).

Figure 5. Playing the video full screen on the iPad

Note that setOrientation method of the MPMoviePlayerController class is not supported on the iPad:

    //---not supported on the iPad---    //[player setOrientation:UIDeviceOrientationPortrait animated:NO];

You can zoom to play the movie full screen by clicking on the two-arrow icons located at the bottom right corner of the player (see Figure 6). However, when the movie is done, a black screen appears and there is no way to get back to the View window.

Figure 6. You can play the video full screen by tapping on the two-arrow icon

Playing Full Screen Movie for iPad

If you want to play a movie full screen on the iPad, you can use the new MPMoviePlayerViewController class. Modify the project as follows:

- (void)viewDidLoad {        NSString *url = [[NSBundle mainBundle]         pathForResource:@"Stock_Footage_Demobroadband"                  ofType:@"mp4"];    MPMoviePlayerViewController *playerViewController =     [[MPMoviePlayerViewController alloc]         initWithContentURL:[NSURL fileURLWithPath:url]];        [[NSNotificationCenter defaultCenter]         addObserver:self           selector:@selector(movieFinishedCallback:)               name:MPMoviePlayerPlaybackDidFinishNotification             object:[playerViewController moviePlayer]];        [self.view addSubview:playerViewController.view];        //---play movie---    MPMoviePlayerController *player = [playerViewController moviePlayer];    [player play];        [super viewDidLoad];    }- (void) movieFinishedCallback:(NSNotification*) aNotification {    MPMoviePlayerController *player = [aNotification object];    [[NSNotificationCenter defaultCenter]         removeObserver:self                  name:MPMoviePlayerPlaybackDidFinishNotification                object:player];    [player stop];    [self.view removeFromSuperView];    [player autorelease];    }

The MPMoviePlayerViewController class is only available on the iPad (SDK 3.2). Like the MPMoviePlayerController class, you register a notification for the player and then add the view exposed by the MPMoviePlayerViewController class to the current View window:

    [self.view addSubview:playerViewController.view];        //---play movie---    MPMoviePlayerController *player = [playerViewController moviePlayer];    [player play];

When the movie has finished playing (or the user has tapped on the Done button), the player is stopped and then removed from the view stack:

- (void) movieFinishedCallback:(NSNotification*) aNotification {    MPMoviePlayerController *player = [aNotification object];    [[NSNotificationCenter defaultCenter]         removeObserver:self                  name:MPMoviePlayerPlaybackDidFinishNotification                object:player];    [player stop];    [self.view removeFromSuperView];    [player autorelease];    }

Press Command-R to test the application. Figure 7 shows the movie playing full screen.

Figure 7. Playing the movie full screen using the MPMoviePlayerViewController class


In this article, you have seen the ways in which you can play video within your iPhone and iPad applications. In particular, you can now embed videos from within your iPad applications!

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as