你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ReplayKit庫,iOS原生直播神器

ReplayKit庫,iOS原生直播神器

編輯:IOS開發綜合

前言

ReplayKit 是WWDC15推出的蘋果原生錄屏 API。在iOS9的時候主要提供的是錄屏,錄制完成後可以進行查看、編輯、通過指定方式分享出去。

在WWDC16上新版的 ReplayKit 提出了了 live 功能,簡單說就是通過 ReplayKit 可以進行錄屏直播。這對於蘋果的手游直播行業有著很重要的意義。

首先給出視頻地址和API文檔

Go Live with ReplayKit - WWDC 2016 ReplayKit API Reference

簡單測試

彈出可以接收廣播的服務列表

新建工程,然後加入ReplayKit.frameword

添加一個按鈕,然後按鈕點擊事件彈出廣播服務的列表:

- (IBAction)displayServiceViewController:(id)sender {
    [RPBroadcastActivityViewController loadBroadcastActivityViewControllerWithHandler:^(RPBroadcastActivityViewController * _Nullable broadcastActivityViewController, NSError * _Nullable error) {
        broadcastActivityViewController.delegate = self;
        [self presentViewController:broadcastActivityViewController animated:YES completion:nil];
    }];
}

 


\

 <喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxoMyBpZD0="mobcrush">Mobcrush

所以想要直播的游戲本身添加這樣一個邏輯,彈出服務列表即可。而直播軟件也只需要注冊為直播服務,就可以直播任何支持的游戲,軟件。國外最火最先支持的就是示例中左邊的Mobcrush,官網,裡面有手機游戲Tower Dash的直播,就是使用這個技術實現的,Tower Dash游戲直播頁面為 - 這裡這裡,需要科學上網。

我錄制了動態圖展示:

 


\

 

動態圖中可以看出支持攝像頭錄制,當然還有麥克風,這些已經滿足了日常主播的基本需求。

國內

國內現在映客直播安裝就直接有注冊為廣播服務,所以截圖中列表裡就有。我還安裝了熊貓TV,虎牙直播,虎牙助手,虎牙手游。熊貓TV的主播權限還沒有申請下來。虎牙手游貌似使用的也是這個技術,但是實現不一樣,虎牙手游直接是在虎牙手游APP內部打開直播,提示成功之後,直接就進入了錄屏模式,然後退出返回到手游界面開始游戲就可以。查看了虎牙直播平台,已經有主播使用了iPhone7進行王者榮耀直播,熊貓TV暫時還沒有看到用iPhone直播手游的。

WWDC 2016

下面是觀看WWDC16 記錄的知識片段。

ReplayKit

新特性:

Apple TV support Live Broadcasting 直播廣播,這個很有用,就是要研究的直播功能 可以記錄 Face Time攝像頭的內容,增強了麥克風記錄API

 


\

 

錄播功能

對於錄播功能之前就已經有了一個典型的demo,可以直接看下面代碼,放進原始功能的第一個viewcontroller裡面就Ok了。


#import "ViewController.h"
#import 

static NSString *StartRecord = @"開始";
static NSString *StopRecord = @"結束";

#if TARGET_IPHONE_SIMULATOR
#define SIMULATOR 1
#elif TARGET_OS_IPHONE
#define SIMULATOR 0
#endif

#define AnimationDuration (0.3)


@interface ViewController () 
{

}
@property (nonatomic, strong)UIButton *btnStart;
@property (nonatomic, strong)UIButton *btnStop;
@property (nonatomic, strong)NSTimer *progressTimer;
@property (nonatomic, strong)UIProgressView *progressView;
@property (nonatomic, strong)UIActivityIndicatorView *activity;
@property (nonatomic, strong)UIView *tipView;
@property (nonatomic, strong)UILabel *lbTip;
@property (nonatomic, strong)UILabel *lbTime;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidAppear:(BOOL)animated {
    BOOL isVersionOk = [self isSystemVersionOk];

    if (!isVersionOk) {
        NSLog(@"系統版本需要是iOS9.0及以上才支持ReplayKit");
        return;
    }
    if (SIMULATOR) {
        [self showSimulatorWarning];
        return;
    }

    UILabel *lb = nil;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;


    //標題
    lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
    lb.font = [UIFont boldSystemFontOfSize:32];
    lb.backgroundColor = [UIColor clearColor];
    lb.textColor = [UIColor blackColor];
    lb.textAlignment = NSTextAlignmentCenter;
    lb.numberOfLines = 3;
    lb.text = @"蘋果ReplayKit Demo";
    lb.center =  CGPointMake(screenSize.width/2, 80);
    [self.view addSubview:lb];

    //創建按鈕
    UIButton *btn = [self createButtonWithTitle:StartRecord andCenter:CGPointMake(screenSize.width/2 - 100, 200)];
    [self.view addSubview:btn];
    self.btnStart = btn;

    btn = [self createButtonWithTitle:StopRecord andCenter:CGPointMake(screenSize.width/2 + 100, 200)];
    [self.view addSubview:btn];
    self.btnStop = btn;
    [self setButton:btn enabled:NO];

    //loading指示
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 80)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor redColor];
    view.layer.cornerRadius = 8.0f;
    view.center = CGPointMake(screenSize.width/2, 300);
    activity.center = CGPointMake(30, view.frame.size.height/2);
    [view addSubview:activity];
    [activity startAnimating];
    self.activity = activity;
    lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 80)];
    lb.font = [UIFont boldSystemFontOfSize:20];
    lb.backgroundColor = [UIColor clearColor];
    lb.textColor = [UIColor blackColor];
    lb.layer.cornerRadius = 4.0;
    lb.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lb];
    self.lbTip = lb;
    self.tipView = view;
    [self hideTip];


    //顯示時間(用於看錄制結果時能知道時間)
    lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    lb.font = [UIFont boldSystemFontOfSize:20];
    lb.backgroundColor = [UIColor redColor];
    lb.textColor = [UIColor blackColor];
    lb.layer.cornerRadius = 4.0;
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init] ;
    [dateFormat setDateFormat: @"HH:mm:ss"];
    NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
    lb.text =  dateString;
    lb.center = CGPointMake(screenSize.width/2, screenSize.height/2 + 100);
    lb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lb];
    self.lbTime = lb;

    //進度條 (顯示動畫,不然看不出畫面的變化)
    UIProgressView *progress = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width*0.8, 10)];
    progress.center = CGPointMake(screenSize.width/2, screenSize.height/2 + 150);
    progress.progressViewStyle = UIProgressViewStyleDefault;
    progress.progress = 0.0;
    [self.view addSubview:progress];
    self.progressView = progress;

    //計時器
    //更新時間
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(updateTimeString)
                                   userInfo:nil
                                    repeats:YES];
}

#pragma mark - UI控件
//顯示 提示信息
- (void)showTipWithText:(NSString *)tip activity:(BOOL)activity{
    [self.activity startAnimating];
    self.lbTip.text = tip;
    self.tipView.hidden = NO;
    if (activity) {
        self.activity.hidden = NO;
        [self.activity startAnimating];
    } else {
        [self.activity stopAnimating];
        self.activity.hidden = YES;
    }
}
//隱藏 提示信息
- (void)hideTip {
    self.tipView.hidden = YES;
    [self.activity stopAnimating];
}

//創建按鈕
- (UIButton *)createButtonWithTitle:(NSString *)title andCenter:(CGPoint)center {

    CGRect rect = CGRectMake(0, 0, 160, 60);
    UIButton *btn = [[UIButton alloc] initWithFrame:rect];
    btn.layer.cornerRadius = 5.0;
    btn.layer.borderWidth = 2.0;
    btn.layer.borderColor = [[UIColor blackColor] CGColor];
    btn.backgroundColor = [UIColor lightGrayColor];
    btn.center = center;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onBtnPressed:) forControlEvents:UIControlEventTouchDown];
    return btn;

}

//設置按鈕是否可點擊
- (void)setButton:(UIButton *)button enabled:(BOOL)enabled {
    if (enabled) {
        button.alpha = 1.0;
    } else {
        button.alpha = 0.2;
    }
    button.enabled = enabled;
}

//提示不支持模擬器
- (void)showSimulatorWarning {
    UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){

    }];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"ReplayKit不支持模擬器" message:@"請使用真機運行這個Demo工程" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:actionCancel];
    [alert addAction:actionOK];

    [self presentViewController:alert animated:NO completion:nil];
}

//顯示彈框提示
- (void)showAlert:(NSString *)title andMessage:(NSString *)message {
    if (!title) {
        title = @"";
    }
    if (!message) {
        message = @"";
    }
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:actionCancel];
    [self presentViewController:alert animated:NO completion:nil];
}

//顯示視頻預覽頁面,animation=是否要動畫顯示
- (void)showVideoPreviewController:(RPPreviewViewController *)previewController withAnimation:(BOOL)animation {

    __weak ViewController *weakSelf = self;

    //UI需要放到主線程
    dispatch_async(dispatch_get_main_queue(), ^{

        CGRect rect = [UIScreen mainScreen].bounds;

        if (animation) {

            rect.origin.x += rect.size.width;
            previewController.view.frame = rect;
            rect.origin.x -= rect.size.width;
            [UIView animateWithDuration:AnimationDuration animations:^(){
                previewController.view.frame = rect;
            } completion:^(BOOL finished){

            }];

        } else {
            previewController.view.frame = rect;
        }

        [weakSelf.view addSubview:previewController.view];
        [weakSelf addChildViewController:previewController];


    });

}

//關閉視頻預覽頁面,animation=是否要動畫顯示
- (void)hideVideoPreviewController:(RPPreviewViewController *)previewController withAnimation:(BOOL)animation {

    //UI需要放到主線程
    dispatch_async(dispatch_get_main_queue(), ^{

        CGRect rect = previewController.view.frame;

        if (animation) {

            rect.origin.x += rect.size.width;
            [UIView animateWithDuration:AnimationDuration animations:^(){
                previewController.view.frame = rect;
            } completion:^(BOOL finished){
                //移除頁面
                [previewController.view removeFromSuperview];
                [previewController removeFromParentViewController];
            }];

        } else {
            //移除頁面
            [previewController.view removeFromSuperview];
            [previewController removeFromParentViewController];
        }
    });
}

#pragma mark - 按鈕 回調
//按鈕事件
- (void)onBtnPressed:(UIButton *)sender {

    //點擊效果
    sender.transform = CGAffineTransformMakeScale(0.8, 0.8);
    float duration = 0.3;
    [UIView animateWithDuration:duration
                     animations:^{
                         sender.transform = CGAffineTransformMakeScale(1.1, 1.1);
                     }completion:^(BOOL finish){
                         [UIView animateWithDuration:duration
                                          animations:^{
                                              sender.transform = CGAffineTransformMakeScale(1.0, 1.0);
                                          }completion:^(BOOL finish){ }];
                     }];

    NSString *function = sender.titleLabel.text;
    if ([function isEqualToString:StartRecord]) {
        [self startRecord];
    }
    else if ([function isEqualToString:StopRecord]) {
        [self stopRecord];
    }
}


- (void)startRecord {

    //    [self setButton:self.btnStart enabled:NO];

    NSLog(@"ReplayKit只支持真機錄屏,支持游戲錄屏,不支持錄avplayer播放的視頻");
    NSLog(@"檢查機器和版本是否支持ReplayKit錄制...");
    if ([[RPScreenRecorder sharedRecorder] isAvailable]) {
        NSLog(@"支持ReplayKit錄制");
    } else {
        NSLog(@"!!不支持支持ReplayKit錄制!!");
        return;
    }

    __weak ViewController *weakSelf = self;

    NSLog(@"%@ 錄制", StartRecord);
    [self showTipWithText:@"錄制初始化" activity:YES];


    [[RPScreenRecorder sharedRecorder] startRecordingWithHandler:^(NSError *error){
        NSLog(@"錄制開始...");
        [weakSelf hideTip];
        if (error) {
            NSLog(@"錯誤信息 %@", error);
            [weakSelf showTipWithText:error.description activity:NO];
        } else {
            //其他處理
            [weakSelf setButton:self.btnStop enabled:YES];
            [weakSelf setButton:self.btnStart enabled:NO];

            [weakSelf showTipWithText:@"正在錄制" activity:NO];
            //更新進度條
            weakSelf.progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.05f
                                                                      target:self
                                                                    selector:@selector(changeProgressValue)
                                                                    userInfo:nil
                                                                     repeats:YES];
        }
    }];

}

- (void)stopRecord {
    NSLog(@"%@ 錄制", StopRecord);

    [self setButton:self.btnStart enabled:YES];
    [self setButton:self.btnStop enabled:NO];

    __weak ViewController *weakSelf = self;
    [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError *  error){


        if (error) {
            NSLog(@"失敗消息:%@", error);
            [weakSelf showTipWithText:error.description activity:NO];
        } else {

            [weakSelf showTipWithText:@"錄制完成" activity:NO];

            //顯示錄制到的視頻的預覽頁
            NSLog(@"顯示預覽頁面");
            previewViewController.previewControllerDelegate = weakSelf;

            //去除計時器
            [weakSelf.progressTimer invalidate];
            weakSelf.progressTimer = nil;

            [self showVideoPreviewController:previewViewController withAnimation:YES];
        }
    }];
}

#pragma mark - 視頻預覽頁面 回調
//關閉的回調
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {
    [self hideVideoPreviewController:previewController withAnimation:YES];
}

//選擇了某些功能的回調(如分享和保存)
- (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet  *)activityTypes {

    __weak ViewController *weakSelf = self;
    if ([activityTypes containsObject:@"com.apple.UIKit.activity.SaveToCameraRoll"]) {

        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf showAlert:@"保存成功" andMessage:@"已經保存到系統相冊"];
        });
    }
    if ([activityTypes containsObject:@"com.apple.UIKit.activity.CopyToPasteboard"]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf showAlert:@"復制成功" andMessage:@"已經復制到粘貼板"];
        });
    }
}

#pragma mark - 計時器 回調

//改變進度條的顯示的進度
- (void)changeProgressValue {
    float progress = self.progressView.progress + 0.01;
    [self.progressView setProgress:progress animated:NO];
    if (progress >= 1.0) {
        self.progressView.progress = 0.0;
    }
}

//更新顯示的時間
- (void)updateTimeString {
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init] ;
    [dateFormat setDateFormat: @"HH:mm:ss"];
    NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
    self.lbTime.text =  dateString;
}

#pragma mark - 其他
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//判斷對應系統版本是否支持ReplayKit
- (BOOL)isSystemVersionOk {
    if ([[UIDevice currentDevice].systemVersion floatValue] < 9.0) {
        return NO;
    } else {
        return YES;
    }
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved