最近在做一个项目时,我需要用 App 音频和麦克风音频录制系统屏幕。
我尝试了以下解决方案。
- 借助 Replay kit 及其函数 startRecording
调用函数之前
recorder.isMicrophoneEnabled = true startRecording(r: recorder)
func startRecording(_ r: RPScreenRecorder) {
r.startRecording(handler: { (error: Error?) -> Void in
if error == nil { // Recording has started
// sender.title = "Stop"
self.recorder.isMicrophoneEnabled = true
} else {
// Handle error
print(error?.localizedDescription ?? "Unknown error")
}
})
}
问题是视频记录并保存到相机胶卷,但保存的视频中既没有 App Audio 也没有 Mic Audio Records
然后我尝试了 Replay Kit 的 screenCapture 功能。代码如下
//MARK: Screen Recording
func startRecording(withFileName fileName: String, recordingHandler:@escaping (Error?)-> Void)
{
if #available(iOS 11.0, *)
{
let fileURL = URL(fileURLWithPath: ReplayFileUtil.filePath(fileName))
// let updatedFileUrl = fileURL.appendingPathExtension(".mp4")
// removeFile(fileURL)
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath : String = "\(documentsDirectory)/Replays/\(fileName)"
if FileManager.default.fileExists(atPath: filePath) {
print("sucess")
}
do {
assetWriter = try? AVAssetWriter.init(url: fileURL, fileType: .mp4)
} catch let error {
print(error.localizedDescription)
}
var videoCleanApertureSettings = [
AVVideoCleanApertureWidthKey : 320,
AVVideoCleanApertureHeightKey : 480,
AVVideoCleanApertureHorizontalOffsetKey : 10,
AVVideoCleanApertureVerticalOffsetKey : 10
]
var codecSettings = [
AVVideoAverageBitRateKey : 960000,
AVVideoMaxKeyFrameIntervalKey : 1,
AVVideoCleanApertureKey : videoCleanApertureSettings
] as [String:Any]
var videoOutputSettings = [
AVVideoCodecKey : AVVideoCodecType.jpeg,
AVVideoCompressionPropertiesKey : codecSettings,
AVVideoWidthKey : 300,
AVVideoHeightKey : 540
] as [String:Any]
var channelLayout = AudioChannelLayout.init()
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_5_1_D
let audioOutputSettings: [String : Any] = [
AVNumberOfChannelsKey: 6,
AVFormatIDKey: kAudioFormatMPEG4AAC_HE,
AVSampleRateKey: 44100,
AVChannelLayoutKey: NSData(bytes: &channelLayout, length: MemoryLayout.size(ofValue: channelLayout)),
]
audioInput = AVAssetWriterInput(mediaType: .audio, outputSettings: audioOutputSettings)
videoInput = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
videoInput.expectsMediaDataInRealTime = true
audioInput.expectsMediaDataInRealTime = true
if assetWriter.canAdd(videoInput) {
print("Added video Input")
assetWriter.add(videoInput)
}
assetWriter.add(audioInput)
self.assetWriter.startWriting()
RPScreenRecorder.shared().isMicrophoneEnabled = true
let time = CMTime.init(value: 10, timescale: 1)
self.assetWriter.startSession(atSourceTime: time)
RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
recordingHandler(error)
if CMSampleBufferDataIsReady(sample)
{
if self.assetWriter.status == AVAssetWriterStatus.unknown
{
// self.assetWriter.startWriting()
print(self.assetWriter.status)
}
print(self.assetWriter.status)
if self.assetWriter.status == AVAssetWriterStatus.failed {
print("Asset Writer failed")
print("Error occured, status = \(self.assetWriter.status.rawValue), \(self.assetWriter.error!.localizedDescription) \(String(describing: self.assetWriter.error))")
return
}
if (bufferType == .video)
{
if self.videoInput.isReadyForMoreMediaData
{
print("Buffer Video Print")
self.videoInput.append(sample)
}
}
if (bufferType == .audioApp || bufferType == .audioMic)
{
if self.audioInput.isReadyForMoreMediaData
{
print("Audio Buffer Came")
self.audioInput.append(sample)
}
}
}
}) { (error) in
recordingHandler(error)
print(error?.localizedDescription)
}
} else
{
// Fallback on earlier versions
}
}
请建议我做错了什么。