HarmonyOS 鸿蒙Next录音组件录音过程实时获取amplitude的问题
HarmonyOS 鸿蒙Next录音组件录音过程实时获取amplitude的问题
我在使用的过程中拿不到值一直报错,使用的代码如下:
try {
let audioManager = audio.getAudioManager();
let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID;
let audioVolumeGroupManager = await audioManager.getVolumeManager().getVolumeGroupManager(groupId)
let audioRoutingManager: audio.AudioRoutingManager = audioManager.getRoutingManager();
let capturerInfo:audio.AudioCapturerInfo = { source:audio.SourceType.SOURCE_TYPE_MIC,capturerFlags : 0 }
let descriptors:audio.AudioDeviceDescriptors = await audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo)
let num = await audioVolumeGroupManager.getMaxAmplitudeForInputDevice(descriptors[0])
console.log(‘分贝:’+num)
} catch (e) {
console.log(’’)
}
麻烦看下上述代码有什么问题一直失败err,或者说有其它api方案可以达到实时获取的效果
async getDeviceInputMaxAmp() {
try {
let audioRoutingManager: audio.AudioRoutingManager = this.audioManager.getRoutingManager();
let descriptors:audio.AudioDeviceDescriptors =
await audioRoutingManager.getPreferredInputDeviceForCapturerInfo(this.audioCapturerOptions.capturerInfo);
let num = await this.audioVolumeGroupManager?.getMaxAmplitudeForInputDevice(descriptors[0]);
console.log('输入分贝: '+num)
} catch (e) {
console.log('分贝: error')
}
}
async getDeviceOutputMaxAmp() {
try {
let audioRoutingManager: audio.AudioRoutingManager = this.audioManager.getRoutingManager();
let descriptors:audio.AudioDeviceDescriptors = await audioRoutingManager
.getPreferOutputDeviceForRendererInfo(this.audioRendererOptions.rendererInfo)
let num = await this.audioVolumeGroupManager?.getMaxAmplitudeForOutputDevice(descriptors[0]);
console.log('输出分贝: '+num)
} catch (e) {
console.log(‘分贝: error’)
}
}
测试代码,注意 getDeviceInputMaxAmp 和 getDeviceOutputMaxAmp 需要 20ms 循环调用,防止两次调用时间间隔较长
正常输出:输入分贝: 0.021576587110757828
async aboutToAppear(): Promise<void> {
console.log(‘NomalCapturer aboutToAppear’);
await this.initResource();
this.audioVolumeGroupManager = await this.audioManager.getVolumeManager().getVolumeGroupManager(audio.DEFAULT_INTERRUPT_GROUP_ID)
setInterval(async () => {
await this.getDeviceInputMaxAmp();
await this.getDeviceOutputMaxAmp();
}, 20);
}
async getDeviceInputMaxAmp() {
try {
let audioRoutingManager: audio.AudioRoutingManager = this.audioManager.getRoutingManager();
let descriptors:audio.AudioDeviceDescriptors =
await audioRoutingManager.getPreferredInputDeviceForCapturerInfo(this.audioCapturerOptions.capturerInfo);
let num = await this.audioVolumeGroupManager?.getMaxAmplitudeForInputDevice(descriptors[0]);
console.log('输入分贝: '+num)
} catch (e) {
console.log(‘分贝: error’)
}
}
async getDeviceOutputMaxAmp() {
try {
let audioRoutingManager: audio.AudioRoutingManager = this.audioManager.getRoutingManager();
let descriptors:audio.AudioDeviceDescriptors = await audioRoutingManager
.getPreferOutputDeviceForRendererInfo(this.audioRendererOptions.rendererInfo)
let num = await this.audioVolumeGroupManager?.getMaxAmplitudeForOutputDevice(descriptors[0]);
console.log('输出分贝: '+num)
} catch (e) {
console.log(‘分贝: error’)
}
}
针对HarmonyOS 鸿蒙Next录音组件录音过程实时获取amplitude的问题,以下是一些解决方案:
在HarmonyOS鸿蒙Next中,若想在录音过程中实时获取amplitude(振幅),可以直接使用AVrecord的getAudioCapturerMaxAmplitude接口。这个接口允许你在录音时获取当前的振幅,从而可以根据振幅大小实现UI动效或其他逻辑处理。
具体实现步骤为:
- 创建AVRecorder实例,并配置好录音参数。
- 开始录音后,使用setInterval或其他定时方法,周期性地调用getAudioCapturerMaxAmplitude接口获取振幅。
- 将获取的振幅用于你的业务逻辑处理,比如更新UI动效。
请注意,由于HarmonyOS的API可能会随着版本更新而发生变化,因此建议查阅最新的官方文档以获取最准确的信息。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。