Unity遊戲需要視頻分享,之前用過第三方的,現在聽說蘋果有了自帶的ReplayKit,畢竟是遊戲中錄製視頻,視頻的大小,壓縮帶來的性能影響,抱着試試看的態度加入了下,性能感覺還可以,但是架不住蘋果的Objective-C和swift啊
#遊戲目標: 爲遊戲增加視頻分享
#開發環境: Unity5.3.0f4
#技術點: ReplayKit,需要ios9.0以上
直接上代碼吧
ReplayKitProxy.h
#import <UIKit/UIKit.h>
#import <ReplayKit/ReplayKit.h>
@interface ReplayKitProxy : NSObject<RPPreviewViewControllerDelegate, RPScreenRecorderDelegate>
+ (ReplayKitProxy*) sharedInstance;
// 是否支持錄像功能(僅ios9以上支持)
+ (BOOL)isSupportReplay;
// 開始錄製視頻
- (void)startRecording;
// 停止錄製視頻
- (void)stopRecording;
// 刪除已錄製視頻,必須在stopRecording之後調用(eg.離開視頻分享界面)
- (void)discardRecording;
// 顯示視頻
- (void)displayRecordingContent;
@end
ReplayKitProxy.m
#import "ReplayKitProxy.h"
@interface ReplayKitProxy () <RPPreviewViewControllerDelegate, RPScreenRecorderDelegate>
@property RPPreviewViewController* previewViewController;
@end
@implementation ReplayKitProxy
+ (ReplayKitProxy *)sharedInstance
{
static ReplayKitProxy* _instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[ReplayKitProxy alloc] init];
});
return _instance;
}
+ (BOOL)isSupportReplay
{
NSString* version = [[UIDevice currentDevice] systemVersion];
BOOL _ios90orNewer = [version compare: @"9.0" options: NSNumericSearch] != NSOrderedAscending;
return _ios90orNewer;
}
// 開始錄製視頻
- (void)startRecording
{
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
if (recorder.available == FALSE) {
NSLog(@"Replaykit is not available");
return;
}
if (recorder.recording == TRUE) {
NSLog(@"Replaykit is recording");
return;
}
// 添加代理:防止有些設備錄製出來黑屏
recorder.delegate = self;
[recorder startRecordingWithMicrophoneEnabled:YES handler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}];
}
// 停止錄製視頻
- (void)stopRecording
{
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
if (recorder.recording == FALSE) {
return;
}
[recorder stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
// [self ShowRecordAlert:error.localizedDescription];
return;
} else {
if (previewViewController != NULL) {
previewViewController.previewControllerDelegate = self;
self.previewViewController = previewViewController;
}
}
}];
}
// 刪除已錄製視頻,必須在stopRecording之後調用
// eg.離開視頻分享界面
- (void)discardRecording
{
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
if (recorder.recording == TRUE) {
return;
}
[recorder discardRecordingWithHandler:^{
NSLog(@"discardRecording complete");
self.previewViewController = NULL;
}];
}
// 顯示視頻
- (void)displayRecordingContent
{
UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:self.previewViewController animated:YES completion:^{
NSLog(@"DIsplay complete!");
}];
}
// MARK: delegate RPPreviewViewControllerDelegate
- (void)previewControllerDidFinish:(RPPreviewViewController*)previewController
{
if (previewController != NULL) {
[previewController dismissViewControllerAnimated:YES completion:^{
}];
}
}
// MARK: RPScreenRecorderDelegate
- (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithError:(NSError *)error previewViewController:(nullable RPPreviewViewController *)previewViewController
{
// Display the error the user to alert them that the recording failed.
// showScreenRecordingAlert(error.localizedDescription)
/// Hold onto a reference of the `previewViewController` if not nil.
if (previewViewController != NULL) {
self.previewViewController = previewViewController;
}
}
@end
Unity Demo下載鏈接:http://pan.baidu.com/s/1ntVRBYh
官方Demo BotsBuildingaCrossPlatformGamewithSpriteKitandGameplayKit下載: http://pan.baidu.com/s/1jGQ8dr4
官方資料:
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