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%')

  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.

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

HarmonyOS
2024-12-27 16:08:17
818浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

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

分享
微博
QQ
微信
回复
2024-12-27 19:38:56


相关问题
HarmonyOS 播放本地音频文件
1385浏览 • 1回复 待解决
使用AudioCapturer开发音频录制功能
2015浏览 • 1回复 待解决
AudioCapturer录音+AudioRenderer播放音频
2377浏览 • 1回复 待解决
HarmonyOS 音频录制播放
1536浏览 • 1回复 待解决
HarmonyOS 获取本地所有音频文件
808浏览 • 1回复 待解决
怎么读取本地音频文件列表?
7312浏览 • 1回复 待解决