繁体   English   中英

录制视频时的音频音量

[英]Volume of audio while recording video

因此,经过大量搜索,我找到了允许在录制视频的同时播放背景音频的代码块。 我在下面粘贴了上述代码块。

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 选项应该是减少音量的唯一选项。 但这个也一样

好的,所以我在深入研究一些文档后找到了答案。

更新后的代码张贴在下面。 您所要做的就是设置 .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")

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM