(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.5播放视频文件
来源:互联网 发布:python的时间加减法 编辑:程序博客网 时间:2024/06/05 18:57
(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook
著作权声明:本文由iam126 翻译,欢迎转载分享。
请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!
相关程序代码下载:http://download.csdn.net/detail/iam126/4068610
或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;
新手翻译,不准确请见谅,参考代码与原书。
1.5播放视频文件
这个 cutscene(cutscene是游戏玩家无法控制的游戏片段,这些片段通常打断正在玩的游戏,以便推进故事情节、说明人物或角色的发展、提供背景信息、氛围、对话和线索。cutscene有两种形式:动画和文字说明)是视频游戏初期所存在的概念。Cutscenes通常穿插于游戏间隔或者游戏的展示界面之中。更多复杂的cutscenes通常都使用动态的视频。在这个教程中,我们将会了解如何在游戏中加入一个视频。
如何工作的…
额外的一个步骤是,需要链接框架-MediaPlayer IOS framework到项目中:
1.右键点击项目中的Groups&Files.
2.点击Add|Existing Frameworks
3.在IOS SDK中选择MediaPlayer.framework
确保在项目中已经链接这个框架。
下面是代码:
#import <MediaPlayer/MediaPlayer.h>
@interface Ch1_PlayingVideoFiles
{
MPMoviePlayerController*moviePlayer;
}
@implementation Ch1_PlayingVideoFiles
-(CCLayer*) runRecipe
{
//Loadour video file
//读取视频文件
NSURL*url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"example_vid" ofType:@"mov"]];
//Createa MPMoviePlayerController object
//创建一个MPMoviePlayerController对象
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:url];
//Registerto receive a notification when the movie has finished playing.
//当视频播放完成时,收到一个通知
//NSNotificationCenter类的方法:-(void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aNameobject:(id)anObject;
[[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//Setthe movie's control style and whether or not it should automatically play.
//设置视频的控制方式以及是否自动播放
//MPMoviePlayerController类的方法:-(void)setFullscreen:(BOOL)fullscreenanimated:(BOOL)animated;
if([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
{
//Usethe new 3.2 style API.
//使用最新的3.2版本的API
moviePlayer.controlStyle= MPMovieControlStyleNone;
//有三种形式:
//1.MPMovieControlStyleNone,
//No controls 毛都没有氏
//2.MPMovieControlStyleEmbedded,
//Controls for an embedded view 嵌入式
//3.MPMovieControlStyleFullscreen,
//Controls for fullscreen playback 全屏式
//默认的时候是第二种
moviePlayer.shouldAutoplay= YES;
CGSizewinSize = [[CCDirector sharedDirector] winSize];
moviePlayer.view.frame= CGRectMake(45, 50, winSize.width-90, winSize.height-100);
[[[CCDirectorsharedDirector] openGLView] addSubview:moviePlayer. view];
}
else
{
//Usethe old 2.0 style API.
//使用2.0版本的API
moviePlayer.movieControlMode= MPMovieControlModeHidden;
//这老版本的2.0的也有三种形式
//1.MPMovieControlModeDefault //默认的
//2.MPMovieControlModeVolumeOnly w//只有音量的
//3.MPMovieControlModeHidden //隐藏的
[selfplayMovie];
}
returnself;
}
-(void)moviePlayBackDidFinish:(NSNotification*)notification
{
//Ifplayback is finished we stop the movie.
//如果重放的话就停止
[selfstopMovie];
}
-(void)playMovie
{
//Wedo not play the movie if it is already playing.
//可以播放的时候还没播放
MPMoviePlaybackStatestate = moviePlayer.playbackState;
if(state== MPMoviePlaybackStatePlaying)
{
NSLog(@"Movieis already playing.");
return;
//如果播放着呢就不管他了
}
[moviePlayerplay];
}
-(void)stopMovie
{
//Wedo not stop the movie if it is already stopped.
//如果已经停了就不用再停一次了
MPMoviePlaybackStatestate = moviePlayer.playbackState;
if(state== MPMoviePlaybackStateStopped)
{
NSLog(@"Movieis already stopped.");
return;
}
//Sinceplayback has finished we remove the observer.
//当回放完成时,我们删除那个通知
[[NSNotificationCenterdefaultCenter] removeObserver:selfname:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//Ifthe moviePlayer.view was added to the openGL view, it needs to be removed.
//如果moviePlayer的界面已经加载进屏幕的话,就给他删掉
if([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
{
[moviePlayer.viewremoveFromSuperview];
}
}
-(void)cleanRecipe
{
[supercleanRecipe];
[selfstopMovie];
[moviePlayerrelease];
}
@end
如何工作的…
这个案例告诉我们如何读取、播放、停止一个视频。
使用MPMoviePlayerController:
这个案例仅仅介绍了有关视频内容的冰山一角。视频也可以在全屏方式下回放,在竖屏状态下,以及很多其他的选项。当你想自定义或者添加这种技术时,可以去看苹果的官方文档。
使用Objective-c的监督机制(observers):
其实监督机制在cocos2d项目中并不经常使用,但是它确实时一种灰常强大的机制,也是一种得知你视频是否完成回放的方法。你可以在一些官方的Objective-c的文档中阅读到更多关于监督机制的内容。
视频文件格式:
通过苹果的文档,你最好使用H.264/MPEG-4、ACC、或者MOV,MP4,MPV,3GP等格式压缩你的视频。
同时,我们也建议你的视频大小不要超过640*480,帧频率不要超过30FPS。
案例中的视频是用苹果电脑的iMovie软件创建并编码的。
更多的信息还是去看官方文档吧~
(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook
著作权声明:本文由iam126 翻译,欢迎转载分享。
请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!
相关程序代码下载:http://download.csdn.net/detail/iam126/4068610
或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;
新手翻译,不准确请见谅,参考代码与原书。
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.5播放视频文件
- 播放视频文件
- (译) Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.1绘制精灵
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.16使用CCParallaxNode
- C#播放各种视频文件
- 用Zaurus播放视频文件
- PB中播放视频文件
- ASP.NET播放视频文件
- AVI 视频文件的播放
- OpenCV 播放视频文件
- flash播放flv视频文件
- 播放Android视频文件
- VB 循环播放视频文件
- iOS 播放flash视频文件
- 网页中播放视频文件
- Smplayer播放wmv视频文件
- iOS 播放flash视频文件
- MPMoviePlayerController-播放视频文件
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.3运动的精灵
- flex---->flex访问java远程对象
- 第五周实验报告(二)
- 在 EmEditor 中通过大纲视图列出 INI 文件中的 Section
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.4绘制OpenGL图形
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.5播放视频文件
- 第五周实验报告(三)
- 闪腰记
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.6网格,粒子,追尾特效
- 第六周实验报告(一)
- APIO07-zoo
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.7使用Retina模式
- 第六周实验报告(二)
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.8 2D舒缓运动