HarmonyOS鸿蒙Next中ArkTS录音文件转mp3

发布于 1周前 作者 eggper 来自 鸿蒙OS

HarmonyOS鸿蒙Next中ArkTS录音文件转mp3 使用ArkTS录音库录制的pcm文件,如何转化成mp3文件

3 回复

使用AVRecorder 参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-media-V5#codecmimetype8

代码如下:

//AvRecorder 封装
export class AvRecorder {
private avRecorder: media.AVRecorder | undefined = undefined;
private avProfile: media.AVRecorderProfile = {
audioBitrate: 100000, // 音频比特率
audioChannels: 2, // 音频声道数
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
audioSampleRate: 48000, // 音频采样率
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a
};

private avConfig: media.AVRecorderConfig = {
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
profile: this.avProfile,
url: '', // 参考应用文件访问与管理开发示例新建并读写一个文件
};

private fd: number = 0;

// 注册audioRecorder回调函数
setAudioRecorderCallback() {
if (this.avRecorder != undefined) {
// 状态机变化回调函数
this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
console.log(`AudioRecorder current state is ${state}`);
})
// 错误上报回调函数
this.avRecorder.on('error', (err: BusinessError) => {
console.error(`AudioRecorder failed, code is ${err.code}, message is ${err.message}`);
})
}
}

// 开始录制对应的流程
async start(filePath: string) {
if (this.avRecorder != undefined) {
await this.avRecorder.release();
this.avRecorder = undefined;
}
// 1.创建录制实例
this.avRecorder = await media.createAVRecorder();
this.setAudioRecorderCallback();
// 2.获取录制文件fd赋予avConfig里的url;参考FilePicker文档
if (fileIo.accessSync(filePath)) {
fileIo.unlinkSync(filePath);
}
let f = fileIo.openSync(filePath, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
this.fd = f.fd;
this.avConfig.url = 'fd://' + f.fd.toString();
// 3.配置录制参数完成准备工作
await this.avRecorder.prepare(this.avConfig);
// 4.开始录制
await this.avRecorder.start();
}

// 暂停录制对应的流程
async pause() {
if (this.avRecorder != undefined && this.avRecorder.state === 'started') { // 仅在started状态下调用pause为合理状态切换
await this.avRecorder.pause();
}
}

// 恢复录制对应的流程
async resume() {
if (this.avRecorder != undefined && this.avRecorder.state === 'paused') { // 仅在paused状态下调用resume为合理状态切换
await this.avRecorder.resume();
}
}

// 停止录制对应的流程
async stop() {
if (this.avRecorder != undefined) {
// 1. 停止录制
if (this.avRecorder.state === 'started'
|| this.avRecorder.state === 'paused') { // 仅在started或者paused状态下调用stop为合理状态切换
await this.avRecorder.stop();
}
// 2.重置
await this.avRecorder.reset();
// 3.释放录制实例
await this.avRecorder.release();
this.avRecorder = undefined;
// 4.关闭录制文件fd
fileIo.close(this.fd);
}
}


// AvRecorder 使用
@State m4aPath: string = getContext(this).cacheDir + "/test.m4a";
private avRecorder = new AvRecorder();


Button("recorder 录制")
.onClick(async () => {
await this.avRecorder.start(this.m4aPath);
})

Button("recorder 暂停")
.onClick(async () => {
await this.avRecorder.pause();
await this.avRecorder.stop();
})

更多关于HarmonyOS鸿蒙Next中ArkTS录音文件转mp3的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,ArkTS提供了@ohos.multimedia.audio模块来处理音频相关操作。要将录音文件转换为MP3格式,可以使用AudioEncoder类进行编码。以下是一个简单的示例代码:

import audio from '@ohos.multimedia.audio';

// 假设已经有一个录音文件路径
let inputFilePath = 'path/to/your/recording.wav';
let outputFilePath = 'path/to/your/output.mp3';

// 创建AudioEncoder实例
let audioEncoder = await audio.createAudioEncoder();

// 配置编码器参数
let encodeOptions = {
    format: audio.AudioEncoderFormat.MP3, // 设置编码格式为MP3
    sampleRate: 44100, // 采样率
    bitRate: 128000, // 比特率
    channelCount: 2 // 声道数
};

// 初始化编码器
await audioEncoder.init(encodeOptions);

// 设置输入文件和输出文件路径
await audioEncoder.setInputFile(inputFilePath);
await audioEncoder.setOutputFile(outputFilePath);

// 开始编码
await audioEncoder.start();

// 编码完成
console.log('录音文件已成功转换为MP3格式');

在HarmonyOS鸿蒙Next中,使用ArkTS实现录音文件转MP3,可以借助第三方库如lamejs。首先,通过@ohos.multimedia.audio模块进行录音,获取原始音频数据。然后,使用lamejs将原始音频数据编码为MP3格式。具体步骤包括:初始化录音器、开始录音、获取音频数据、调用lamejs进行编码、保存MP3文件。确保在开发环境中配置好相关依赖,并处理音频数据的格式转换和编码过程。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!