iOS9 ReplayKit录制视频


Unity游戏需要视频分享,之前用过第三方的,现在听说苹果有了自带的ReplayKit,毕竟是游戏中录制视频,视频的大小,压缩带来的性能影响,抱着试试看的态度加入了下,性能感觉还可以,但是架不住苹果的Objective-C和Swift


#游戏目标: 为游戏增加视频分享


#开发环境: Unity5.3.0f4


#技术点: ReplayKit,需要ios9.0以上


直接上代码吧

ReplayKitProxy.h

[objc]  view plain  copy
  1. #import <UIKit/UIKit.h>  
  2. #import <ReplayKit/ReplayKit.h>  
  3.   
  4. @interface ReplayKitProxy : NSObject<RPPreviewViewControllerDelegate, RPScreenRecorderDelegate>  
  5.   
  6. + (ReplayKitProxy*) sharedInstance;  
  7.   
  8. // 是否支持录像功能(仅ios9以上支持)  
  9. + (BOOL)isSupportReplay;  
  10.   
  11. // 开始录制视频  
  12. - (void)startRecording;  
  13.   
  14. // 停止录制视频  
  15. - (void)stopRecording;  
  16.   
  17. // 删除已录制视频,必须在stopRecording之后调用(eg.离开视频分享界面)  
  18. - (void)discardRecording;  
  19.   
  20. // 显示视频  
  21. - (void)displayRecordingContent;  
  22.   
  23. @end  

ReplayKitProxy.m

[objc]  view plain  copy
  1. #import "ReplayKitProxy.h"  
  2.   
  3. @interface  ReplayKitProxy () <RPPreviewViewControllerDelegate, RPScreenRecorderDelegate>  
  4. @property RPPreviewViewController* previewViewController;  
  5. @end  
  6.   
  7.   
  8. @implementation ReplayKitProxy  
  9.   
  10. + (ReplayKitProxy *)sharedInstance  
  11. {  
  12.     static ReplayKitProxy* _instance = nil;  
  13.     static dispatch_once_t onceToken;  
  14.     dispatch_once(&onceToken, ^{  
  15.         _instance = [[ReplayKitProxy alloc] init];  
  16.     });  
  17.     return _instance;  
  18. }  
  19.   
  20. + (BOOL)isSupportReplay  
  21. {  
  22.     NSString* version = [[UIDevice currentDevice] systemVersion];  
  23.       
  24.     BOOL _ios90orNewer = [version compare@"9.0" options: NSNumericSearch] != NSOrderedAscending;  
  25.       
  26.     return _ios90orNewer;  
  27. }  
  28.   
  29. // 开始录制视频  
  30. - (void)startRecording  
  31. {  
  32.     RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;  
  33.       
  34.     if (recorder.available == FALSE) {  
  35.         NSLog(@"Replaykit is not available");  
  36.         return;  
  37.     }  
  38.       
  39.     if (recorder.recording == TRUE) {  
  40.         NSLog(@"Replaykit is recording");  
  41.         return;  
  42.     }  
  43.       
  44.     // 添加代理:防止有些设备录制出来黑屏  
  45.     recorder.delegate = self;  
  46.       
  47.     [recorder startRecordingWithMicrophoneEnabled:YES handler:^(NSError * _Nullable error) {  
  48.         if (error) {  
  49.             NSLog(@"%@", error.localizedDescription);  
  50.         }  
  51.     }];  
  52. }  
  53.   
  54. // 停止录制视频  
  55. - (void)stopRecording  
  56. {  
  57.     RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;  
  58.     if (recorder.recording == FALSE) {  
  59.         return;  
  60.     }  
  61.       
  62.     [recorder stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error) {  
  63.         if (error) {  
  64.             NSLog(@"%@", error.localizedDescription);  
  65.             // [self ShowRecordAlert:error.localizedDescription];  
  66.             return;  
  67.         } else {  
  68.             if (previewViewController != NULL) {  
  69.                 previewViewController.previewControllerDelegate = self;  
  70.                 self.previewViewController = previewViewController;  
  71.             }  
  72.         }  
  73.     }];  
  74. }  
  75.   
  76. // 删除已录制视频,必须在stopRecording之后调用  
  77. // eg.离开视频分享界面  
  78. - (void)discardRecording  
  79. {  
  80.     RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;  
  81.     if (recorder.recording == TRUE) {  
  82.         return;  
  83.     }  
  84.       
  85.     [recorder discardRecordingWithHandler:^{  
  86.         NSLog(@"discardRecording complete");  
  87.         self.previewViewController = NULL;  
  88.     }];  
  89. }  
  90.   
  91. // 显示视频  
  92. - (void)displayRecordingContent  
  93. {  
  94.     UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;  
  95.     [rootViewController presentViewController:self.previewViewController animated:YES completion:^{  
  96.         NSLog(@"DIsplay complete!");  
  97.     }];  
  98. }  
  99.   
  100. // MARK: delegate RPPreviewViewControllerDelegate  
  101. - (void)previewControllerDidFinish:(RPPreviewViewController*)previewController  
  102. {  
  103.     if (previewController != NULL) {  
  104.         [previewController dismissViewControllerAnimated:YES completion:^{  
  105.               
  106.         }];  
  107.     }  
  108. }  
  109.   
  110. // MARK: RPScreenRecorderDelegate  
  111. - (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithError:(NSError *)error previewViewController:(nullable RPPreviewViewController *)previewViewController  
  112. {  
  113.     // Display the error the user to alert them that the recording failed.  
  114. //    showScreenRecordingAlert(error.localizedDescription)  
  115.       
  116.     /// Hold onto a reference of the `previewViewController` if not nil.  
  117.     if (previewViewController != NULL) {  
  118.         self.previewViewController = previewViewController;  
  119.     }  
  120. }  
  121.   
  122. @end  


# 注意点 : ReplayKit.framework必须ios9.0以上才支持,所以需要将framework的属性设置为Optional,否则在9.0以下手机上会报错





Unity Demo下载链接:http://pan.baidu.com/s/1ntVRBYh

官方Demo BotsBuildingaCrossPlatformGamewithSpriteKitandGameplayKit下载: http://pan.baidu.com/s/1jGQ8dr4


官方资料:

Apple文档:https://developer.apple.com/library/ios/documentation/ReplayKit/Reference/ReplayKit_Collection/index.html#//apple_ref/doc/uid/TP40016260

WWDC 15 Session video: Going Social with ReplayKit and Game Center

Sample code: DemoBots: Building a Cross Platform Game with SpriteKit and GameplayKit


参考blog:

iOS9 ReplayKit录制视频  http://blog.csdn.net/cocos2der/article/details/50260873

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值