ios - 录制视频时的音频音量
问题描述
因此,经过大量搜索后,我找到了允许在录制视频的同时播放背景音频的代码块。我在下面粘贴了上述代码块。
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")
}
}
推荐阅读
- android - 如何在自定义适配器内生成的按钮中设置一些操作
- python - 拒绝在 Python 中从 Dropbox API 下载文件的权限
- java - Java 8 Streams 多个分组方式
- javascript - 如何串行运行基于承诺的功能
- point-cloud-library - 如何使用 pcl 将 3D 点云离散化为 xy 平面上的 2D 网格,而不是使用体素网格?
- php - 当我在 laravel 5.1 API 中使用邮递员运行 API 时,发布方法不起作用
- python - Python列表取决于长度
- java - 如何从分布式环境访问位于 S3 存储桶中的 DBFS 文件?
- r - 在 R 中使用 Data.table 中的 Data.table
- javascript - 当资源太大时,如何绑定事件处理程序以请求关闭套接字的承诺?