在ionic 3中将音频文件转换为base64 [英] convert audio file to base64 in ionic 3

查看:340
本文介绍了在ionic 3中将音频文件转换为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to save an audio and send it to the server in ionic 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.

After stopping the record i want to send the audio file in base64 to the server. so I am using the base64 plugin to convert the audio file to base64.

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

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

However, using this plugin i get base64File with the below value:

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...

所以你知道我在做什么错吗?

So any idea what am i doing wrong?

推荐答案

在本文中使用我的答案.这是一个官方图书馆. Ionic 3从录制的文件中获取base64音频字符串

Use my answer in this post. It is an official library. Ionic 3 get base64 audio string from recorded file

要将插件添加到您的项目中:

To add the plugin in your project:

  1. 使用:ionic cordova plugin add com-badrit-base64添加 插件
  2. 使用:npm install @ionic-native/base64添加模块 在您的proyect和package.json
  3. 以如下方式导入您的module.ts导入库:import { Base64 } from "@ionic-native/base64";

  1. Use:ionic cordova plugin add com-badrit-base64 to add the pluggin.
  2. Use:npm install @ionic-native/base64 to add the module in your proyect and your package.json
  3. Import in your module.ts importing the libary like:import { Base64 } from "@ionic-native/base64";

将其添加到提供程序JSON中.

Add it in the providers JSON.

providers:[
      Camera,
      Media,
      File,
      SpeechRecognition,
      Base64,
],

  • 在组件的构造函数中声明此新库:private base64:Base64,

    使用该方法.

    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;
    });
    

  • 这篇关于在ionic 3中将音频文件转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆