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

问题描述 投票:1回答:1

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

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

示例应用程序: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
swift avplayer avaudiosession
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.