文档开发者中心
我的
|

HarmonyOS 6

多声道融合方案咨询

已解决
大家好,我最近在开发一个音乐播放类应用,需要用两首歌进行合成,比如一个是背景音乐,另一个是人说话的声音,两个是独立的音频,如何合并成一个?
点赞
收藏
回复
1
分享
举报
浏览2310 发布于2026-03-26 07:09山东
全部评论
最多点赞
最新发布
最早发布
|
采纳答复

【解决方案】

可以使用三方库@ohos/mp4parser调用ffmpeg指令来实现音频合成,参考代码:

收起
自动换行
深色代码主题
复制
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { ICallBack, MP4Parser } from '@ohos/mp4parser';

@Component
@Entry
struct mergerAudio{
  build() {
    Column(){
      Button('音频保存到沙箱').onClick(()=>{
        let context = this.getUIContext().getHostContext() as Context;
        let dir: string = context.cacheDir
        // 确保在工程目录src/main/resources/rawfile里存在input.pdf文档
        let filePath: string = dir + '/testbayinhe.mp3';
        try {
          let res = fileIo.accessSync(filePath);
          if (!res) {
            let content: Uint8Array = context.resourceManager.getRawFileContentSync('rawfile/testbayinhe.mp3');
            let fdSand = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
            fileIo.writeSync(fdSand.fd, content.buffer);
            fileIo.closeSync(fdSand.fd);
          }
        } catch (e) {
          let error: BusinessError = e as BusinessError;
          hilog.error(0x0000, 'IndexPage', `Code: ${error.code}, message: ${error.message} `);
        }
        let filePath2: string = dir + '/qingyinyue.mp3';
        try {
          let res = fileIo.accessSync(filePath2);
          if (!res) {
            let content2: Uint8Array = context.resourceManager.getRawFileContentSync('rawfile/qingyinyue.mp3');
            let fdSand2 = fileIo.openSync(filePath2, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
            fileIo.writeSync(fdSand2.fd, content2.buffer);
            fileIo.closeSync(fdSand2.fd);
          }
        } catch (e) {
          let error: BusinessError = e as BusinessError;
          hilog.error(0x0000, 'IndexPage', `Code: ${error.code}, message: ${error.message} `);
        }
      })
      Button('合成音频').onClick(()=>{
        let context = this.getUIContext().getHostContext() as Context;
        let getLocalDirPath = context.cacheDir+"/";
        let filePathOne = getLocalDirPath + "testbayinhe.mp3";
        let filePathTwo = getLocalDirPath + "qingyinyue.mp3";
        let callback: ICallBack = {
          callBackResult: (code: number) => {
            if (code === 0) {
              console.info('Mixed audio succeed');
            } else {
              console.info('Mixed audio failed');
            }
          },
        };
        let outPath = getLocalDirPath + "mergeout.mp3";
        let cmd =
          `ffmpeg -i ${filePathTwo} -i ${filePathOne} -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp3 ${outPath}`;
        MP4Parser.ffmpegCmd(cmd, callback);
      })
    }.margin({top:100})
  }
}
展开全部
1楼回复于2026-03-26 08:53 来自广东
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。