Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Playing Video on the iPhone and iPad : Page 2

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


advertisement

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

Summary

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!


Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap