多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > ReplayKit庫,iOS原生直播神器

ReplayKit庫,iOS原生直播神器

來源:程序員人生   發布時間:2016-10-22 13:36:16 閱讀次數:3797次

版權聲明:本文為博主原創,如需轉載請注明出處。

前言

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

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

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

  • Go Live with ReplayKit - WWDC 2016
  • ReplayKit API Reference

簡單測試

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

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

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

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



Mobcrush

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

我錄制了動態圖展現:



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

國內

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

WWDC 2016

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

ReplayKit

新特性:

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



錄播功能

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

#import "ViewController.h" #import <ReplayKit/ReplayKit.h> 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 () <RPPreviewViewControllerDelegate> { } @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 <NSString *> *)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; } } @end

新博客文章地址:ReplayKit庫,iOS原生直播神器

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日韩大片在线观看 | 亚洲一级视频在线观看 | 老司机午夜在线视频免费 | 欧美日韩一区二区三区视频在线观看 | 女人一级特纯黄大片色 | 男人都懂的www网站免费观看 | 国产成人在线免费视频 | 久久嫩草 | 一级毛片短视频 | 自拍网址 | 日本免费中文字幕在线看 | 欧美性猛交aa一级 | 精品一区二区三区自拍图片区 | 日韩欧美国产中文字幕 | jizzjizz中国丝袜美女 | 午夜国产精品不卡在线观看 | 欧美αv天堂在线视频 | 午夜a级片 | 亚洲免费在线观看视频 | 精品一久久香蕉国产二月 | 亚洲欧美日韩中文字幕网址 | 国产不卡高清 | 天天天天鲁天天拍一拍 | 最近中文字幕2019免费版日本 | 免费毛片全部不收费的 | 成年人性生活免费视频 | 日韩精品欧美激情亚洲综合 | 18jzz大全中文| 亚洲精品一区亚洲精品 | 亚洲欧洲日本精品 | 日本69av | 国产福利自产拍在线观看 | 国产产一区二区三区久久毛片国语 | 国产日韩欧美综合一区二区三区 | 亚洲伊人久久大香线蕉综合图片 | 亚洲国产精品成 | 日本午夜小视频 | 国产中文字幕在线观看 | 一区二区精品在线观看 | 17videosex性欧美 | 欧洲乱码专区一区二区三区四区 |