HarmonyOS AudioCapturer录音后的文件无法播放

文件名后缀为wav,将文件从手机拷贝到windows电脑中,使用potpalayer播放,提示无法播放。

代码如下:

import { audio } from '@kit.AudioKit'
import fs from '@ohos.file.fs'

class AudioCaptureUtil {
  private audioCapturer?: audio.AudioCapturer
  async pcmToMp3(outputFilePath: string) {
    let audioStreamInfo: audio.AudioStreamInfo = {
      samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, // 采样率16000Hz
      channels: audio.AudioChannel.CHANNEL_2, // 单声道
      sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式16bit
      encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW, // PCM编码
    }
    // 音频采集
    this.audioCapturer = await audio.createAudioCapturer({
      streamInfo: audioStreamInfo,
      capturerInfo: {
        source: audio.SourceType.SOURCE_TYPE_MIC,
        capturerFlags: 0
      }
    })
    let file2 = fs.openSync(outputFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    this.audioCapturer.on('stateChange', (state) => {
      if (state === audio.AudioState.STATE_PREPARED) {
        this.audioCapturer?.start()
      }
    })
    let bufferSize = 0
    this.audioCapturer.on('readData', (buf2) => {
      fs.writeSync(file2.fd, buf2, {
        offset: bufferSize,
        length: buf2.byteLength
      })
      bufferSize += buf2.byteLength
    })
  }

  async stop() {
    await this.audioCapturer?.stop()
    await this.audioCapturer?.release()
    this.audioCapturer = undefined
  }
}
let audioCaptureUtil = new AudioCaptureUtil()
export default audioCaptureUtil as AudioCaptureUtil

后缀名试过mp3,pcm,wav,m4a都不行。这是后缀名的问题吗?要如何才能保存能正确格式的音频?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

目前只支持pcm,文档地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/audio-kit-intro-V5#%E6%94%AF%E6%8C%81%E7%9A%84%E9%9F%B3%E9%A2%91%E6%A0%BC%E5%BC%8F

如果需要转其他格式,请参考三方库:https://gitee.com/xiangyuecn/Recorder提供转码支持,允许将录制的buffers数据或任意pcm数据转码成需要的格式。

分享
微博
QQ
微信
回复
2天前
相关问题
AudioCapturer录音+AudioRenderer播放音频
1544浏览 • 1回复 待解决
HarmonyOS 录音和语音播放相关demo
15浏览 • 1回复 待解决
HarmonyOS 录音上传文件
205浏览 • 1回复 待解决
HarmonyOS 录音,希望能有录音Demo
462浏览 • 1回复 待解决