因?yàn)轫?xiàng)目里剛好需要錄制屏幕的功能,就找了下iOS上的錄制相關(guān)的SDK,由于我們的錄制需求比較簡單,所以選擇官方的ReplayKit。
ReplayKit是蘋果官方很晚(iOS 9)才推出的錄制屏幕的SDK,所以整個(gè)SDK顯得比較簡單,而且可自定義的程度很低。如果你的APP只是需要錄制功能,對界面和功能方面沒有特別高的要求,那么這個(gè)SDK才會適合。
具體實(shí)現(xiàn):
開始錄制
- (void)beginRecord {
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
if(!recorder.isAvailable) {
[self postMessage:@"您的手機(jī)無法使用錄制功能"];
return;
}
if(recorder.isRecording) {
[self postMessage:@"正在錄制中,請先暫停"];
return;
}
recorder.microphoneEnabled = YES;
recorder.cameraEnabled = YES; //錄制期間攝像頭和麥克風(fēng)都保持開啟狀態(tài)
if (@available(iOS 10.0, *)) { //iOS9和iOS10的啟動方法都是不一樣的
[recorder startRecordingWithHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"啟動成功");
}
}];
} else {
if (@available(iOS 9.0, *)) {
[recorder startRecordingWithMicrophoneEnabled:YES handler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"啟動成功");
}
}];
}
}
}
通過kvo方式監(jiān)聽什么時(shí)候正式啟動,啟動時(shí)間和手機(jī)有關(guān),不過都還是挺快的:
- (void)addRecordObserver {
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
[recorder addObserver:self forKeyPath:@"recording" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if([keyPath isEqualToString:@"recording"]) {
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
if(recorder.isRecording) {
# 注意: ReplayKit的相關(guān)方法基本都不是在主線程執(zhí)行,操作UI需要切換到主線程。
dispatch_async(dispatch_get_main_queue(), ^{
[_recordStartButton setTitle:@"錄制中" forState:UIControlStateNormal];
_recordStartButton.backgroundColor = [UIColor redColor];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
_recordStartButton.backgroundColor = [UIColor whiteColor];
[_recordStartButton setTitle:@"錄制" forState:UIControlStateNormal];
});
}
}
}
停止錄制
- (void)recordStopClicked {
RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
if(!recorder.isRecording) {
return;
}
__weak typeof(self) weakSelf = self;
[recorder stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error) { //RPPreviewViewController是錄制之后的預(yù)覽界面
if(!error) {
[weakSelf showVideoPreviewController:previewViewController withAnimation:YES];
}
NSLog(@"已停止錄屏");
}];
}
- (void)showVideoPreviewController:(RPPreviewViewController *)previewController withAnimation:(BOOL)animation {
previewController.previewControllerDelegate = self; //設(shè)置預(yù)覽界面代理,主要是按鈕點(diǎn)擊后的回調(diào)
[self presentViewController:previewController animated:YES completion:^{
}];
}
實(shí)現(xiàn)代理方法:
#代理方法也很不友好,
#兩個(gè)方法在點(diǎn)擊保存和取消按鈕的時(shí)候都會觸發(fā)
#第二個(gè)方法可以知道點(diǎn)擊了按鈕的類型,但是每次都會攜帶曾經(jīng)點(diǎn)擊過得所有按鈕的Types,所以很難對每個(gè)事件進(jìn)行單獨(dú)處理。
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {
[previewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet <NSString *> *)activityTypes {
dispatch_async(dispatch_get_main_queue(), ^{
if ([activityTypes containsObject:@"com.apple.UIKit.activity.SaveToCameraRoll"]) { //點(diǎn)擊了保存按鈕
NSLog(@"已保存到相冊");
}
});
}
總結(jié)
ReplayKit還很不成熟,優(yōu)缺點(diǎn)也很明顯。
優(yōu)點(diǎn):
1、使用簡單,代碼量很少。
2、性能高,錄制期間感覺不到任何影響。
3、視頻質(zhì)量好,大小也能接受,iPhoneXS錄制的7秒視頻容量大概是500KB。
缺點(diǎn):
1、自定義程度低,預(yù)覽界面完全無法自定義。
2、開放方法很少,甚至連錄制后的視頻URL都沒開放,不過知道屬性名可以通過分類獲取到,也可以通過KVC獲取。
2、錄制有時(shí)候會啟動不了,此時(shí)可以測試系統(tǒng)自帶的錄制功能,如果也不行,那就只能重啟手機(jī),沒有其他方案。