iOS app启动播放视频实现

来源:互联网 发布:赛德斯绝地求生编程 编辑:程序博客网 时间:2024/05/04 17:19

http://blog.csdn.net/piao_polar/article/details/8923828


背景

    一般的app,启动的时候是显示一张底图的。但运营提出要播放一段视频容易给人新奇的感觉,以留住新尝鲜的玩家;


实现

    和以往一样,先在外网机做了demo,本次修改见 https://github.com/piaopolar/iOS_Samples/commit/eebdee731ac4def6ee98374a66d49bf1775c2247


1. iOS播放视频的方法,样式控制

   要播放视频,使用MPMoviePlayerViewController,并处理播放事件的相关回调即可,见代码中ViewController.h/m的修改

   但通常这是不满足需求的,还有视频的全屏样式,去除原生的工具栏,旋转方向控制等需求,见代码中AppDelegate.h/m的修改,要点如下:

a) 全屏,去除工具栏

[plain] view plaincopy
  1. moviePlayerController.moviePlayer.view.frame = self.viewController.view.bounds;  
  2. moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleNone;  
  3. moviePlayerController.moviePlayer.scalingMode = MPMovieScalingModeFill;  


   
b) 旋转方向控制,比如这个视频我们可能只希望进行横屏的显示,而在设备竖立着的时候不旋转成竖着的。

   从MPMoviePlayerViewController派生,并实现shouldAutorotate/supportedInterfaceOrientations
[plain] view plaincopy
  1. @interface LandscapeMPMoviePlayerViewController : MPMoviePlayerViewController  
  2. @end  
  3.   
  4. @implementation LandscapeMPMoviePlayerViewController  
  5. -(BOOL) shouldAutorotate  
  6. {  
  7.   return YES;  
  8. }  
  9.   
  10. -(NSUInteger)supportedInterfaceOrientations  
  11. {  
  12.   return UIInterfaceOrientationMaskLandscape;  
  13. }  
  14. @end      



   可能需要对视频本身进行旋转

[plain] view plaincopy
  1. [moviePlayerController.view setTransform:CGAffineTransformMakeRotation(2 * M_PI - M_PI / 2)];   



2. 程序启动这个的特殊时刻的处理

   相关讨论见 http://stackoverflow.com/questions/7051208/emulating-splash-video-in-ios-application/

   其实无论是直接把moviePlayerController作为rootViewController,或者是作为其子界面都可以。

   demo中为了更纯粹的处理moviePlayerController的旋转,用了前者,而对于一个已经成型的有了屏幕旋转策略的游戏来说,可能直接AddSubView作为子窗口更加省事。要注意的要点主要有:

a. window和rootViewController的初始化时刻
[plain] view plaincopy
  1. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  2.   
  3. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {  
  4.     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];  
  5. } else {  
  6.     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];  
  7. }   



b. 只有调用了makeKeyAndVisible才能看到显示

[plain] view plaincopy
  1. [self.window makeKeyAndVisible];   



   至此,已经能实现策划的需求了,其他细节可以直接看demo:git://github.com/piaopolar/iOS_Samples.git


   

其他:

1. 关于模拟器/黑苹果

   这个功能本身是可以用模拟器做测试的。我在远程的Mac Mini机上开发的好好的,把工程移到专网的黑苹果机器上就发现播放视频时候会报错

Error 'obj!' trying to fetch default input device's simple rate
Error getting audio input device sample rate:'!obj'

   联系到黑苹果上一直听不到声音,并且看不到声音设备。我去下载了个声卡驱动给黑苹果装上,虽然还是没法正常听见声音,可是在系统中能看到声卡了,也不在报错了。

2. 启动视频要和启动图片在视觉效果上要一致,避免突兀。类似大掌门的启动图片直接就是一整张黑色的图片,而启动视频本身也是黑色背景,淡入淡出看起来就自然。

3. 在做这个功能还遇到了一个小插曲,从有外网的远程测试机把代码迁移到专网机器,发现会报一个这样的异常:NSInvalidUnarchiveOperationException: Could not instantiate class named NSLayoutConstraint

   google后得知是由于远程机上XCode版本比较高,而专网开发机上XCode版本比较低导致的。详细讨论和解决方法见:http://stackoverflow.com/questions/13201586/nsinvalidunarchiveoperationexception-could-not-instantiate-class-named-nslayout

   看看人家stackoverflow的人多热情,为了解答问题专门录屏编辑再转为gif图,再看看国内csdn那些只会说去查MSDN差google的货,真心感觉到差距。

0 0
原创粉丝点击