Playing Video on the iPhone and iPad

Playing Video on the iPhone and iPad,第1张

概述  Playing Video on the iPhone and iPad 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 iPho

 

Playing VIDeo on the iPhone and iPad
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]             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,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. 来源:http://www.devx.com/wireless/Article/44642/1954 总结

以上是内存溢出为你收集整理的Playing Video on the iPhone and iPad全部内容,希望文章能够帮你解决Playing Video on the iPhone and iPad所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存