IOS SWIFT-在播放视频时记录音频,并且仅通过麦克风记录

时间:2019-11-27 16:26:00

标签: swift avplayer avaudiosession

我有一个应用程序,可以在用户同时播放视频时记录用户的音频会话,问题是来自扬声器的视频回放。

理想情况下,我想抵消来自扬声器的音频,仅通过麦克风记录用户的声音。

示例应用程序: FaceTime 当您进行FaceTime通话时,Facetime会取消您正在播放的音乐,并且仅记录用户的语音(https://www.quora.com/How-does-an-iPhone-cancel-out-the-music-you-are-playing-when-you-FaceTime-someone

下面是我的用于添加音频会话以记录用户的代码:

        let audioInputDevice = AVCaptureDevice.default(.builtInMicrophone, for: .audio, position: .unspecified)
            .default(for: AVMediaType.audio)!
        let session = self.previewLiveCamera.cameraLayer!.session!

        //TODO: fix Audio Input, do not mix with other video audio
        do {
            audioInput = try AVCaptureDeviceInput(device: audioInputDevice!)
            session.addInput(audioInput!)
        } catch {
            print("failed to add audio input")
        }

        for output in session.outputs {
            let connectionOutput = output as! AVCaptureMovieFileOutput
            self.connectionOutput = connectionOutput
            print("Started reacording to \(fileURL!)")
            self.connectionOutput.startRecording(to: fileURL!, recordingDelegate: self)

        }

此外,我为用户同时观看的视频设置了视频播放器:

let postPlayer = AVPlayer(url: postVideoUrl!)
cell.playerLayer.player = postPlayer

1 个答案:

答案 0 :(得分:0)

添加输入音频源后,您需要设置音频模式:

    do {
        audioInput = try AVCaptureDeviceInput(device: audioInputDevice!)
        session.addInput(audioInput!)
    } catch {
        print("failed to add audio input")
    }

// Add the following lines:
try! AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .voiceChat)
try! AVAudioSession.sharedInstance().setActive(true)

.voiceChat将使设备的音调均衡针对语音进行优化,并且允许的音频路由集减少到仅适合语音聊天的音频路由

https://developer.apple.com/documentation/avfoundation/avaudiosession/mode/1616455-voicechat

如果在设置类别后,音频仍然有过多的扬声器音频,则需要查看启用主动回声消除[AEC]。这是在iOS下执行此操作的示例:

https://github.com/twilio/video-quickstart-ios/blob/master/AudioDeviceExample/AudioDevices/ExampleAVAudioEngineDevice.m#L802请注意VoiceProcessingIO等。