|
| 1 | +// |
| 2 | +// ScreenRecorder.swift |
| 3 | +// BugReporterTest |
| 4 | +// |
| 5 | +// Created by Giridhar on 09/06/17. |
| 6 | +// Copyright © 2017 Giridhar. All rights reserved. |
| 7 | +// |
| 8 | +import Foundation |
| 9 | +import ReplayKit |
| 10 | +import AVKit |
| 11 | + |
| 12 | + |
| 13 | +class ScreenRecorder |
| 14 | +{ |
| 15 | + var assetWriter:AVAssetWriter! |
| 16 | + var videoInput:AVAssetWriterInput! |
| 17 | + |
| 18 | + let viewOverlay = WindowUtil() |
| 19 | + |
| 20 | + //MARK: Screen Recording |
| 21 | + func startRecording(withFileName fileName: String, recordingHandler:@escaping (Error?)-> Void) |
| 22 | + { |
| 23 | + if #available(iOS 11.0, *) |
| 24 | + { |
| 25 | + |
| 26 | + let fileURL = URL(fileURLWithPath: ReplayFileUtil.filePath(fileName)) |
| 27 | + assetWriter = try! AVAssetWriter(outputURL: fileURL, fileType: |
| 28 | + AVFileType.mp4) |
| 29 | + let videoOutputSettings: Dictionary<String, Any> = [ |
| 30 | + AVVideoCodecKey : AVVideoCodecType.h264, |
| 31 | + AVVideoWidthKey : UIScreen.main.bounds.size.width, |
| 32 | + AVVideoHeightKey : UIScreen.main.bounds.size.height |
| 33 | + ]; |
| 34 | + |
| 35 | + videoInput = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings) |
| 36 | + videoInput.expectsMediaDataInRealTime = true |
| 37 | + assetWriter.add(videoInput) |
| 38 | + |
| 39 | + RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in |
| 40 | +// print(sample,bufferType,error) |
| 41 | + |
| 42 | + recordingHandler(error) |
| 43 | + |
| 44 | + if CMSampleBufferDataIsReady(sample) |
| 45 | + { |
| 46 | + if self.assetWriter.status == AVAssetWriterStatus.unknown |
| 47 | + { |
| 48 | + self.assetWriter.startWriting() |
| 49 | + self.assetWriter.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(sample)) |
| 50 | + } |
| 51 | + |
| 52 | + if self.assetWriter.status == AVAssetWriterStatus.failed { |
| 53 | + print("Error occured, status = \(self.assetWriter.status.rawValue), \(self.assetWriter.error!.localizedDescription) \(String(describing: self.assetWriter.error))") |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + if (bufferType == .video) |
| 58 | + { |
| 59 | + if self.videoInput.isReadyForMoreMediaData |
| 60 | + { |
| 61 | + self.videoInput.append(sample) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + }) { (error) in |
| 67 | + recordingHandler(error) |
| 68 | +// debugPrint(error) |
| 69 | + } |
| 70 | + } else |
| 71 | + { |
| 72 | + // Fallback on earlier versions |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + func stopRecording(handler: @escaping (Error?) -> Void) |
| 77 | + { |
| 78 | + if #available(iOS 11.0, *) |
| 79 | + { |
| 80 | + RPScreenRecorder.shared().stopCapture |
| 81 | + { (error) in |
| 82 | + handler(error) |
| 83 | + self.assetWriter.finishWriting |
| 84 | + { |
| 85 | + print(ReplayFileUtil.fetchAllReplays()) |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + } else { |
| 90 | + // Fallback on earlier versions |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +} |
0 commit comments