ArticleDirectory
- Playing video on the iPhone
- Playing video on the iPad
- Playing full screen movie for iPad
- Summary
This article discusses the two classes supported in the iPhone SDK for video playback.
By Wei-Meng Lee
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] handler: [nsurl fileurlwithpath: url]; [[nsicationicationcenter defacenter center] addobserver: Self selector: @ selector (handler :) name: Your object: player]; // --- play movie --- [player play]; [Super viewdidload];}-(void) moviefinishedcallback :( nsnotification *) anotification {mpmovieercontroller * player = [anotification object]; [[nsnotifcenter center defacenter center] 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):
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (moviefinishedcallback :) name: mpmovieplayerplaybackdidfinishnotification object: Player];
When the movie stops playing, you shoshould unregister the notification and then release the Player Object:
-(Void) moviefinishedcallback :( nsnotification *) anotification {mpmovieplayercontroller * player = [anotification object]; [[nsicationicationcenter defacenter center] removeobserver: Self name: icationobject: 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: @ "canonical" oftype: @ "MP4"]; mpmovieplayercontroller * player = [[mpmovieplayercontroller alloc] initwithcontenturl: [nsurl fileurlwithpath: url]; [[nsicationcenter center defacenter center] addobserver: Self selector: @ selector (moviefinishedcallback :) name: jsonobject: Player]; // --- play partial screen --- player. view. frame = cgrectmake (184,200,400,300); [self. view addsubview: Player. view]; // --- play movie --- [player play]; [Super viewdidload];}
Essential, 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: @ "canonical" oftype: @ "MP4"]; mpmovieplayerviewcontroller * playerviewcontroller = [[mpmovieplayerviewcontroller alloc] callback: [nsurl fileurlwithpath: url]; [[nsnotifcenter center defacenter center] addobserver: Self selector: @ selector (moviefinishedcallback :) name: jsonobject: [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]; [[nsicationicationcenter defacenter center] removeobserver: Self name: mpmovieplayerplaybackdidfinishnotification object: Player]; [Player stop]; [self. view removefromsuperview]; [Player autorelease];}
The mpmovieplayerviewcontroller class is only available on the iPad (sdks 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]; [[nsicationicationcenter defacenter center] removeobserver: Self name: icationobject: 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 particle, you can now embed videos from within your iPad applications!
Wei-Meng LeeIs 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. 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 .