HarmonyOS 音频文件base64编解码问题

音频文件格式为m4a,经过base64本地编码和base64本地解码保存后,文件无法打开。

HarmonyOS
2024-12-25 11:53:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

参考示例:

import { BusinessError } from '@ohos.base';
import { common } from '@kit.AbilityKit';
import { util } from '@kit.ArkTS';
import fs from '@ohos.file.fs';

@Entry
@Component struct base64video{
  private context = getContext(this) as common.UIAbilityContext
  private base: string = ''
  private base_1: string = ''

  encodeAudio() {
    try {
      this.context.resourceManager.getRawFileContent("ttt.m4a").then((value: Uint8Array) => {
        let rawFile = value;
        this.base = new util.Base64Helper().encodeToStringSync(rawFile)
      }).catch((error: BusinessError) => {
        console.error("getRawFileContent promise error is " + error);
      });
    } catch (error) {
      let code = (error as BusinessError).code;
      let message = (error as BusinessError).message;
      console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`);
    }
  }
  decodeAudio() {
    let pathDir = this.context.filesDir;
    let filePath = pathDir + `/test.m4a`
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    let result = new util.Base64Helper().decodeSync(this.base);
    let writeLen = fs.writeSync(file.fd, result.buffer)
    fs.close(file)
  }

  async encodeAgain() {
    let pathDir = this.context.filesDir;
    let filePath = pathDir + `/test.m4a`
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    let stat = fs.statSync(file.fd)
    let buf = new ArrayBuffer(stat.size)
    await fs.read(file.fd, buf)
    this.base_1 = new util.Base64Helper().encodeToStringSync(new Uint8Array(buf))
    fs.close(file)
  }

  decodeAgain() {
    let pathDir = this.context.filesDir;
    let filePath = pathDir + `/test_1.m4a`
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    let result = new util.Base64Helper().decodeSync(this.base_1);
    let writeLen = fs.writeSync(file.fd, result.buffer)
    fs.close(file)
  }

  build(){
    Column(){
      Button('音频编码').onClick(()=>{ this.encodeAudio() })
      Button('音频解码并保存').onClick(()=>{ this.decodeAudio() })
      Button('重复编码').onClick(()=>{ this.encodeAgain() })
      Button('重复解码并保存').onClick(()=>{ this.decodeAgain() })
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 12:35:22
相关问题
HarmonyOS base64编解码
1004浏览 • 0回复 待解决
HarmonyOS base64解码报错
1967浏览 • 1回复 待解决
HarmonyOS base64解码内容缺少
630浏览 • 1回复 待解决
HarmonyOS 音频编解码问题咨询
922浏览 • 1回复 待解决
HarmonyOS base64编码问题
1401浏览 • 1回复 待解决
照片文件Base64
636浏览 • 1回复 待解决
base64怎么转换为PDF文件
256浏览 • 0回复 待解决
HarmonyOS 图片Base64编码
843浏览 • 1回复 待解决
HarmonyOS 图片转base64
950浏览 • 1回复 待解决
HarmonyOS base64转image
774浏览 • 1回复 待解决
HarmonyOS 如何将文件流转为base64
1173浏览 • 1回复 待解决
HarmonyOS 网络传输BASE64转义问题
873浏览 • 1回复 待解决
HarmonyOS 关于base64和aes加密相关问题
767浏览 • 1回复 待解决
HarmonyOS 音视频编解码-音频渲染
1307浏览 • 1回复 待解决
HarmonyOS Image加载base64失败
973浏览 • 1回复 待解决
HarmonyOS pixelMap怎么转base64
627浏览 • 1回复 待解决
HarmonyOS base64编码的API
756浏览 • 1回复 待解决
HarmonyOS arraybuffer转base64乱码
873浏览 • 1回复 待解决