录制视频时的音频音量

4

经过大量的搜索,我终于找到了允许背景音频在录制视频时播放的代码块,以下是该代码块。

fileprivate func setBackgroundAudioPreference() {
    guard allowBackgroundAudio == true else {
        return
    }

    guard audioEnabled == true else {
        return
    }

    do{
        if #available(iOS 10.0, *) {
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .allowBluetooth, .allowAirPlay, .allowBluetoothA2DP])
        } else {
            let options: [AVAudioSession.CategoryOptions] = [.mixWithOthers, .allowBluetooth]
            let category = AVAudioSession.Category.playAndRecord
            let selector = NSSelectorFromString("setCategory:withOptions:error:")
            AVAudioSession.sharedInstance().perform(selector, with: category, with: options)
        }
        try AVAudioSession.sharedInstance().setActive(true)
        session.automaticallyConfiguresApplicationAudioSession = false
    }
    catch {
        print("[SwiftyCam]: Failed to set background audio preference")

    }
}

然而,我有一个小问题。某种原因导致相机加载时背景音频音量降低。当我使用 Instagram 录制视频时,音频不会被降低,并且仍然录制。有没有办法更改我的当前代码块,使其在录制视频时不降低音量?
我阅读了文档,显然 .duckOthers 选项应该是唯一降低音量的选项。但这个选项也会降低音量。
1个回答

3

好的,我在深入查阅一些文档后找到了答案。

下面是更新后的代码。您只需要设置 .defaultToSpeaker 选项即可。

fileprivate func setBackgroundAudioPreference() {
    guard allowBackgroundAudio == true else {
        return
    }

    guard audioEnabled == true else {
        return
    }

    do{
        if #available(iOS 10.0, *) {
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .allowBluetooth, .allowAirPlay, .allowBluetoothA2DP,.defaultToSpeaker])
        } else {
            let options: [AVAudioSession.CategoryOptions] = [.mixWithOthers, .allowBluetooth]
            let category = AVAudioSession.Category.playAndRecord
            let selector = NSSelectorFromString("setCategory:withOptions:error:")
            AVAudioSession.sharedInstance().perform(selector, with: category, with: options)
        }
        try AVAudioSession.sharedInstance().setActive(true)
        session.automaticallyConfiguresApplicationAudioSession = false
    }
    catch {
        print("[SwiftyCam]: Failed to set background audio preference")

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接