RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Playing Video on the iPhone and iPad

This article discusses the two classes supported in the iPhone SDK for video playback.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 <MediaPlayer/MediaPlayer.h> @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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date