经过大量的搜索,我终于找到了允许背景音频在录制视频时播放的代码块,以下是该代码块。
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 选项应该是唯一降低音量的选项。但这个选项也会降低音量。