1.录音的时候设置语音格式为amr
function startRecord() {
startTimestamp = (new Date()).getTime();
r = null;
r = plus.audio.getRecorder();
r.record({
format:"amr",
filename: "_doc/audio/"
}, function(p) {
console.log(p);
rurl = p;
if(IsSendAudio){
//执行发送方法
sendAudio(rurl);
}else{
plus.io.resolveLocalFileSystemURL(rurl,function(entry){
entry.remove();
},function(e){})
}
}, function(e) {
alert("Audio record failed: " + e.message);
});
}
2.base64转语音文件到本地
//将base64编码转换成录音文件
dataURL2Audio = function(base64Str, callback) {
var myArray=new Array();
var myArray = base64Str.split(";base64,");
console.log(myArray[1]+"base64Str");
base64Str=myArray[1];
// var base64Str = base64Str.replace('data:audio/amr;base64,', '');
var audioName = (new Date()).valueOf() + '.amr';
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
fs.root.getFile(audioName, {
create: true
}, function(entry) {
// 获得平台绝对路径
var fullPath = entry.fullPath;
if(mui.os.android) {
// 读取音频
var Base64 = plus.android.importClass("android.util.Base64");
var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
try {
var out = new FileOutputStream(fullPath);
var bytes = Base64.decode(base64Str, Base64.DEFAULT);
console.log(bytes+"-------")
out.write(bytes);
out.close();
// 回调
callback && callback(entry);
} catch(e) {
console.log(e.message);
}
} else if(mui.os.ios) {
var NSData = plus.ios.importClass('NSData');
var nsData = new NSData();
nsData = nsData.initWithBase64EncodedStringoptions(base64Str,0);
nsData.plusCallMethod({writeToFile:fullPath,atomically:true});
plus.ios.deleteObject(nsData);
// 回调
callback && callback(entry);
}
})
})
}
调用方式:
dataURL2Audio(base64Str, function(entry) {
var content = entry.toURL();
console.log(content)
})