在iOS 11.0以上的系统中,ReplayKit支持了实时流的录制,iOS11前的系统只支持将录制的视频保存在本地沙盒。
//开启录屏模式
[[RPScreenRecorder sharedRecorder] startCaptureWithHandler:^(CMSampleBufferRef _Nonnull sampleBuffer, RPSampleBufferType bufferType, NSError * _Nullable error) {
if (!CMSampleBufferDataIsReady(sampleBuffer))
{
return;
}
//RPSampleBufferTypeVideo = 1,视屏声音
// RPSampleBufferTypeAudioApp,App的声音
// RPSampleBufferTypeAudioMic,Mic录制的声音
switch (bufferType) {
case RPSampleBufferTypeVideo:
{
//视屏帧 可执行上传的操作
}
break;
case RPSampleBufferTypeAudioMic:
{
//mic音频帧
}
break;
default:
break;
}
} completionHandler:^(NSError * _Nullable error) {
}];
关闭录屏
//关闭录屏
[[RPScreenRecorder sharedRecorder] stopCaptureWithHandler:^(NSError * _Nullable error) {
}];
代理方法RPScreenRecorderDelegate
//可能因为某种原因报错
- (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithError:(NSError *)error previewViewController:(nullable RPPreviewViewController *)previewViewController API_DEPRECATED("No longer supported", ios(9.0, 10.0), tvos(10.0,10.0));
- (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithPreviewViewController:(nullable RPPreviewViewController *)previewViewController error:(nullable NSError *)error API_AVAILABLE(ios(11.0), tvos(11.0));
我现在在录屏,这时候切到后台录屏的线程断开了,但是任务还存在,因为没有停止掉,所以我在delegate里面执行了关闭录屏,在回调中重新打开录屏,录屏流恢复上传
[screenRecorder stopCaptureWithHandler:^(NSError * _Nullable error) {
//重新打开录屏
}]