【Unity3D游戏开发】iOS9 ReplayKit录制视频 (三十)


【Unity3D游戏开发】iOS9 ReplayKit录制视频 (三十)_第1张图片


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


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


#开发环境: Unity5.3.0f4


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


直接上代码吧

ReplayKitProxy.h

#import 
#import 

@interface ReplayKitProxy : NSObject

+ (ReplayKitProxy*) sharedInstance;

// 是否支持录像功能(仅ios9以上支持)
+ (BOOL)isSupportReplay;

// 开始录制视频
- (void)startRecording;

// 停止录制视频
- (void)stopRecording;

// 删除已录制视频,必须在stopRecording之后调用(eg.离开视频分享界面)
- (void)discardRecording;

// 显示视频
- (void)displayRecordingContent;

@end

ReplayKitProxy.m

#import "ReplayKitProxy.h"

@interface  ReplayKitProxy () 
@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


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




【Unity3D游戏开发】iOS9 ReplayKit录制视频 (三十)_第2张图片

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




你可能感兴趣的:(【Unity3D游戏开发】)