将音频文件转换为离子3中的base64

问题描述 投票:0回答:3

我正在尝试保存音频并将其发送到离子3中的服务器。

record(){
  if (this.platform.is('ios')) {
    this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new 
    Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new 
    Date().getSeconds()+'.3gp';
    this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + 
    this.fileName;
    this.audio = this.media.create(this.filePath);
  } else if (this.platform.is('android')) {
    this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new 
    Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new 
    Date().getSeconds()+'.3gp';
    this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') 
    + this.fileName;
    this.audio = this.media.create(this.filePath);
  }
    this.audio.startRecord();
    this.recording = true;
}

在停止记录后,我想将base64中的音频文件发送到服务器。所以我使用base64插件将音频文件转换为base64。

this.base64.encodeFile(this.filePath).then((base64File: string) => {
            // let audiooo = encodeURIComponent(base64File);
            this.sendVoice(encodeURIComponent(base64File));
        }, (err) => {
          console.log(err);
        });

但是,使用这个插件我得到base64File具有以下值:

data%3Aimage%2F%3Bcharset%3Dutf-8%3Bbase64%2C%2f%2FFsQBIF%2FAFAC....

I cannot play this encoded audio after sending it to the server, i believe it is because the file starts data%3Aimage... not with data%3Aaudio...

所以任何想法我做错了什么?

angular typescript ionic-framework ionic3
3个回答
1
投票

在这篇文章中使用我的答案。这是一个官方图书馆。 Ionic 3 get base64 audio string from recorded file

要在项目中添加插件:

  1. 使用:qazxsw poi添加插件。
  2. 使用:ionic cordova plugin add com-badrit-base64在你的proyect和package.json中添加模块
  3. 导入你的qazxsw poi导入库如:qazxsw poi
  4. 将它添加到提供程序JSON中。 npm install @ionic-native/base64
  5. 在组件的构造函数中声明这个新库:module.ts
  6. 使用该方法。 import { Base64 } from "@ionic-native/base64";

0
投票

我有不同的解决方案!因为基本的原生bas64无法正常工作

我有这个惊人的providers:[ Camera, Media, File, SpeechRecognition, Base64, ], ,我以前用它做音频转换到base 64 //将音频MP3编码为Base64 var pathToFile =“/ pathToFile / audio.mp3”;

private base64:Base64,

0
投票

你可以用this.base64.encodeFile(this.filePath + this.fileName).then( (base64:any) =>{ console.log('file base64 encoding: ' + base64); var x = base64.substr(13,base64.length); x = "data:audio/mpeg;base64" + x; }); 实现这个目的:

library
© www.soinside.com 2019 - 2024. All rights reserved.