使用麦克风音频录制屏幕视频

时间:2019-08-15 15:57:44

标签: ios swift xcode avfoundation replaykit

我有一个带有麦克风音频的代码记录屏幕。当我们使用RPScreenRecorder.shared().startCapture时,完成处理程序将返回包含视频和音频数据的样本缓冲区。共有三种类型的缓冲区(视频,应用程序的音频,麦克风的音频)。如果打开应用程序,一切正常,那么我们将拥有所有类型的采样缓冲区,甚至包括来自麦克风的音频,但是如果我们关闭应用程序并再次打开它,则除了来自麦克风的音频之外,还将具有所有类型的采样缓冲区。我真的需要这个音频。

import Foundation
import ReplayKit

class ScreenRecorder {

    public static let shared = ScreenRecorder()
    private var isRecording = false


    func startRecording() {

        guard !isRecording else {
            return
        }

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
        } catch let error as NSError {
            print(error.localizedDescription)
        }

        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch let error as NSError {
            print(error.localizedDescription)
        }

        RPScreenRecorder.shared().isMicrophoneEnabled = true
        RPScreenRecorder.shared().startCapture(handler: { (cmSampleBuffer, rpSampleBufferType, error) in

            switch rpSampleBufferType {
            case .video:
                print("___video")
            case .audioApp:
                print("___audio app")
            case .audioMic:
                print("___audio mic")
            default:
                break
            }
        }) { error in
            print(error?.localizedDescription)
        }
    }

    func stopRecording() {
        RPScreenRecorder.shared().stopCapture { error in
            print("stop capture")
        }
    }
}

我希望所有类型的样本缓冲区。

0 个答案:

没有答案