HarmonyOS 鸿蒙Next AudioCapturer录音数据处理问题
HarmonyOS 鸿蒙Next AudioCapturer录音数据处理问题
import { audio } from "@kit.AudioKit";
import { Queue } from '@kit.ArkTS';
import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
export class GroupCallSendVioceManager {
private static instance: GroupCallSendVioceManager;
private constructor() {
// 私有构造函数,防止外部实例化
}
public static getInstance(): GroupCallSendVioceManager {
if (!GroupCallSendVioceManager.instance) {
GroupCallSendVioceManager.instance = new GroupCallSendVioceManager();
}
return GroupCallSendVioceManager.instance;
}
public startGroupCall():void{
this.startRecorder()
}
public stopGroupCall():void{
this.stopRecorder();
}
/**
* 开始录音采集
* @param filePath 录音存放路径
*/
async startRecorder() {
// 1. 创建音频采集器
const audioCapturer = await this.getAudioCapturer()
let path = getContext().filesDir;
let bufferSize: number = 0;
let filePath = path + '/MyVoice.pcm';
this.destFile = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE | fs.OpenMode.TRUNC);
// 2. 订阅(读取音频采集器的数据流,写入到打开的文件中)
audioCapturer.on('readData', async (buffer) => {
this.shortQueue.add(buffer)
if(this.shortQueue.length > 2){
let array1 = new Int16Array(this.shortQueue.pop());
let array2 = new Int16Array(this.shortQueue.pop());
let array3 = new Int16Array(this.shortQueue.pop());
let mergedArray = new Int16Array(array1.length + array2.length + array3.length);
mergedArray.set(array1);
mergedArray.set(array2, array1.length);
mergedArray.set(array3 , array1.length + array2.length)
let options: ReadOptions = {
offset: bufferSize,
length: mergedArray.buffer.byteLength
}
fs.writeSync(this.destFile?.fd, mergedArray.buffer, options);
bufferSize += mergedArray.buffer.byteLength;
}
})
// 3. 开始
audioCapturer.start()
}
// 结束录音采集
async stopRecorder(){
if(this.audioCapturer != null){
// 获取音频采集器
const audioCapturer = await this.getAudioCapturer()
// 1. 停止录音
await audioCapturer.stop()
// 2. 释放资源和硬件占用
audioCapturer.release()
// 3. 释放变量,对象重新赋值为 null,可以被 垃圾回收机制 自动清理
this.audioCapturer = null
}
fs.close(this.destFile)
}
// 音频采集器实例
private audioCapturer: audio.AudioCapturer | null = null
// 音频流配置
private audioStreamInfo: audio.AudioStreamInfo = {
samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_8000, // 采样率
channels: audio.AudioChannel.CHANNEL_1, // 通道
sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式
};
// 音频采集器配置
private audioCapturerInfo: audio.AudioCapturerInfo = {
source: audio.SourceType.SOURCE_TYPE_MIC, //音源类型:Mic音频源
capturerFlags: 0//音频采集器标志。0代表普通音频采集器,1代表低时延音频采集器。
};
//创建/获取音频采集器
async getAudioCapturer() {
//如果有的话直接返回,避免占用系统资源
if (this.audioCapturer) {
return this.audioCapturer
}
//关键API audio.createAudioCapturer
this.audioCapturer = await audio.createAudioCapturer({
streamInfo: this.audioStreamInfo,
capturerInfo: this.audioCapturerInfo
})
return this.audioCapturer
}
}
如上面,audioCapturer.on('readData', async (buffer) => {}
中,buffer
转为Int16Array
写入.pcm
文件,用第三方应用播放.pcm
文件,音质会变成电音,buffer
直接写入.pcm
文件播放声音正常,各位大佬看看什么问题,因为我们需要对.pcm
进行编解码,所以buferr
传入native层,需要转为int16array
更多关于HarmonyOS 鸿蒙Next AudioCapturer录音数据处理问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next AudioCapturer录音数据处理问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对HarmonyOS(鸿蒙)Next AudioCapturer录音数据处理问题,以下提供直接且专业的回答:
在HarmonyOS中,使用Next AudioCapturer进行录音时,录音数据的处理主要涉及数据的获取与后续处理流程。确保你已经正确初始化并配置了AudioCapturer对象,包括设置正确的音频格式、采样率、通道数等参数。
录音数据通常通过回调函数或异步事件获取。在获取到数据后,你可能需要进行编码、压缩或实时处理。处理时需注意数据格式与你的处理逻辑是否匹配,例如PCM格式数据需按字节或帧进行解析。
若遇到数据处理错误或异常,首先检查数据源(麦克风)是否正常工作,其次验证AudioCapturer的配置是否正确。此外,还需确保系统权限(如录音权限)已正确申请并授予。
对于复杂的数据处理需求,如实时音频分析、降噪处理等,可能需要借助第三方库或自行实现算法。在算法实现时,需考虑性能与资源消耗,确保在鸿蒙设备上能够流畅运行。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html