我正在尝试保存音频并将其发送到离子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...
所以任何想法我做错了什么?
在这篇文章中使用我的答案。这是一个官方图书馆。 Ionic 3 get base64 audio string from recorded file
要在项目中添加插件:
ionic cordova plugin add com-badrit-base64
在你的proyect和package.json中添加模块npm install @ionic-native/base64
module.ts
import { Base64 } from "@ionic-native/base64";
我有不同的解决方案!因为基本的原生bas64无法正常工作
解
我有这个惊人的providers:[
Camera,
Media,
File,
SpeechRecognition,
Base64,
],
,我以前用它做音频转换到base 64
//将音频MP3编码为Base64 var pathToFile =“/ pathToFile / audio.mp3”;
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;
});
实现这个目的:
library