HarmonyOS 如何实现录音功能?

如题:HarmonyOS 如何实现录音功能?

阅读 529
1 个回答

录音功能核心代码:

// 开始录制对应的流程
async startRecordingProcess() {
  try {
    if (this.avRecorder == undefined) {
      // 1.创建录制实例
      this.avRecorder = await media.createAVRecorder();
    }
    this.setAudioRecorderCallback();
    // 2.获取录制文件fd赋予avConfig里的url;参考FilePicker文档
    this.curFile = fileIo.openSync(this.filesDir + '/Audio_' + new Date().getTime() + '.mp4', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
    this.avConfig.url = 'fd://' + this.curFile.fd;
    // 3.配置录制参数完成准备工作
    await this.avRecorder.prepare(this.avConfig);
    // 4.开始录制
    this.textTimerController.start()
    await this.avRecorder.start();
    this.recordFlag = true;
  } catch (err) {
    console.log(TAG, 'startRecordingProcess' + JSON.stringify(err))
  }
}

可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/using-avrecorder-for-recording-V5