HarmonyOS AudioCapturer录制的音频文件无法播放

import audioUtil from '../common/AudioUtil'
import audio from '@ohos.multimedia.audio';
import { BusinessError } from '@ohos.base';
import fs from '@ohos.file.fs';
class Options {
  offset?: number;
  length?: number;
}
@Entry
@Component
struct audioTest {
  audioCapturer: audio.AudioCapturer | undefined = undefined;
  bufferSize: number = 0;
  TAG: string = 'audioTest';
  aboutToAppear(): void {
    this.init()
  }
  readonly readDataCallback = (buffer: ArrayBuffer) => {
    let path = getContext().cacheDir;
    let filePath = path + '/StarWars10s-2C-48000-4SW.wav';
    let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    let options: Options = {
      offset: this.bufferSize,
      length: buffer.byteLength
    }
    fs.writeSync(file.fd, buffer, options);
    this.bufferSize += buffer.byteLength;
  }
  async init() {
    // 获取权限
    await audioUtil.checkMicPermission()
    // 初始化
    audio.createAudioCapturer({
      streamInfo: {
        samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率
        channels: audio.AudioChannel.CHANNEL_2, // 通道
        sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式
        encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式
      },
      capturerInfo: {
        source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型
        capturerFlags: 0 // 音频采集器标志
      }
    }, (err, capturer) => { // 创建AudioCapturer实例
      if (err) {
        console.error(`Invoke createAudioCapturer failed, code is ${err.code}, message is ${err.message}`);
        return;
      }
      console.info(`${this.TAG}: create AudioCapturer success`);
      this.audioCapturer = capturer;
      if (this.audioCapturer !== undefined) {
        (this.audioCapturer as audio.AudioCapturer).on('readData', this.readDataCallback);
      }
    });
  }
  // 开始一次音频采集
  start() {
    if (this.audioCapturer !== undefined) {
      let stateGroup = [audio.AudioState.STATE_PREPARED, audio.AudioState.STATE_PAUSED, audio.AudioState.STATE_STOPPED];
      if (stateGroup.indexOf((this.audioCapturer as audio.AudioCapturer).state.valueOf()) === -1) { // 当且仅当状态为STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一时才能启动采集
        console.error(`${this.TAG}: start failed`);
        return;
      }
      // 启动采集
      (this.audioCapturer as audio.AudioCapturer).start((err: BusinessError) => {
        if (err) {
          console.error('Capturer start failed.');
        } else {
          console.info('Capturer start success.');
        }
      });
    }
  }
  // 停止采集
  stop() {
    if (this.audioCapturer !== undefined) {
    // 只有采集器状态为STATE_RUNNING或STATE_PAUSED的时候才可以停止
    if ((this.audioCapturer as audio.AudioCapturer).state.valueOf() !== audio.AudioState.STATE_RUNNING && (this.audioCapturer as audio.AudioCapturer).state.valueOf() !== audio.AudioState.STATE_PAUSED) {
      console.info('Capturer is not running or paused');
      return;
    }
    //停止采集
    (this.audioCapturer as audio.AudioCapturer).stop((err: BusinessError) => {
      if (err) {
        console.error('Capturer stop failed.');
      } else {
        console.info('Capturer stop success.');
      }
    });
    }
  }
  build() {
    Column() {
      Button('开始录音')
        .onClick(() => {
          console.log('charles start')
          this.start()
        })
        .margin({ top: 100 })
      Button('结束录音')
        .onClick(() => {
          console.log('charles stop')
          this.stop()
        })
    }
    .width('100%')
    .height('100%')

  }
}

录制完成后的StarWars10s-2C-48000-4SW.wav文件,从IDE的Device File Browser从沙盒中复制到电脑上,使用播放器播放,无法正常播放,显示格式损坏。

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

audioCapture生成的音频文件是PCM格式原始数据,需要音频输出后添加数据处理才能播放。验证阶段可以把录音文件导出来,用三方软件比如Audacity转为可以播放的格式。

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 播放本地音频文件
284浏览 • 1回复 待解决
使用AudioCapturer开发音频录制功能
1318浏览 • 1回复 待解决
HarmonyOS 音频录制播放
670浏览 • 1回复 待解决
AudioCapturer录音+AudioRenderer播放音频
1368浏览 • 1回复 待解决
怎么读取本地音频文件列表?
6372浏览 • 1回复 待解决
HarmonyOS raw音频文件读取转为字节流
27浏览 • 1回复 待解决
HarmonyOS 音频文件base64编解码问题
60浏览 • 1回复 待解决