ios - 将 AVAudioMixerNode 连接到 AVAudioEngine
问题描述
我使用 AVAudioMixerNode 来更改音频格式。这个条目对我帮助很大。下面的代码给了我想要的数据。但是我在手机的扬声器上听到了我自己的声音。我该如何预防?
func startAudioEngine()
{
engine = AVAudioEngine()
guard let engine = engine, let input = engine.inputNode else {
// @TODO: error out
return
}
let downMixer = AVAudioMixerNode()
//I think you the engine's I/O nodes are already attached to itself by default, so we attach only the downMixer here:
engine.attach(downMixer)
//You can tap the downMixer to intercept the audio and do something with it:
downMixer.installTap(onBus: 0, bufferSize: 2048, format: downMixer.outputFormat(forBus: 0), block: //originally 1024
{ (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
//i get audio data here
}
)
//let's get the input audio format right as it is
let format = input.inputFormat(forBus: 0)
//I initialize a 16KHz format I need:
let format16KHzMono = AVAudioFormat.init(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 11025.0, channels: 1, interleaved: true)
//connect the nodes inside the engine:
//INPUT NODE --format-> downMixer --16Kformat--> mainMixer
//as you can see I m downsampling the default 44khz we get in the input to the 16Khz I want
engine.connect(input, to: downMixer, format: format)//use default input format
engine.connect(downMixer, to: engine.outputNode, format: format16KHzMono)//use new audio format
engine.prepare()
do {
try engine.start()
} catch {
// @TODO: error out
}
}
解决方案
您可以通过扬声器听到您的麦克风录音,因为您的麦克风已连接到downMixer
,它已连接到engine.outputNode
。downMixer
如果您不将其与其他输入一起使用,您可能只是将输出静音:
downMixer.outputVolume = 0.0
推荐阅读
- c# - 在文件夹中另存为 iText.Layout.Element.Image
- macos - 如何在 SwiftUI 中自动扩展 NSTextView 的高度?
- datetime - 如何在mule 4中转换日期?
- python - 我是 python 新手,我在研究假新闻检测算法,我在从 url 中提取网站名称时遇到问题
- python - Discord.py 类型错误:on_message() 缺少 1 个必需的仅关键字参数:“搜索”
- sql - 在 Postgres 中存储站点的开放时间
- ip - 我们可以用 IP 地址做什么?
- java - 如何修改部署在不同国家的spring boot rest api的业务逻辑
- mysql - 如何使用 MySQL 查询去除具有相同颜色或大小的重复记录
- angular - 如何在 Doughnutchart ng2 chart.js 中自定义百分比标签