录制视频时的音频音量 [英] Volume of audio while recording video

查看:144
本文介绍了录制视频时的音频音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So after a lot of searching I was able to find the code block that allows background audio to play while at the same time recording video. I have pasted said code block below.

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录制视频时,音频不会降低,并且仍然记录,有什么方法可以更改我的当前代码块,以在使用视频重新编码时不降低音量?

However, I have one small issue. For some reason when the camera loads the background Audio volume is reduced. When I record a video with instagram the audio doesn't get reduced and it still records is there any way I can change my current code block to not lower the volume while recoding with video?

我阅读了文档,显然.duckOthers选项应该是减小音量的唯一选项.但是这个也很好

I read the documentation and apparently .duckOthers option should be the only option that reduces the volume. But this one does as well

推荐答案

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

Okay so I found the answer after diving further into some of the documentation.

下面发布了更新的代码.您所需要做的就是设置.defaultToSpeaker选项

Updated code posted below. All you have to do is set the .defaultToSpeaker option

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")

    }
}

这篇关于录制视频时的音频音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆