逻辑层
onLoad(options) {
let record = '_doc/uniapp_save/1585051885897.mp3';
let bgm = '_doc/uniapp_save/1585051885897.mp3';
this.fileToArrayBuffer(record).then(res=>{
this.option.type = 1;
this.option.recordBase64 = res;
this.fileToArrayBuffer(bgm).then(res=>{
this.option.type = 2;
this.option.recordBase64 = res;
}).catch(err=>{
});
}).catch(err=>{
});
},
//将文件转为base64
fileToArrayBuffer(filePath){
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(filePath,(entry) => {
// 可通过entry对象操作文件
entry.file((file)=>{
var fileReader = new plus.io.FileReader();
fileReader.readAsDataURL(file,'utf-8');
fileReader.onloadend = (event) => {
//base64
let base64 = event.target.result;;
resolve(base64);
}
});
}, function ( err ) {
this.$humor.msg(JSON.stringify(err));
reject(err);
});
});
},
渲染层
// 监听 service 层数据变更
updateData(newValue, oldValue, ownerInstance, instance) {
if(newValue.type==1){
if(newValue.recordBase64){
// console.log("处理录音音频",newValue);
this.dataURLtoArray(newValue.recordBase64).then(arrayBuffer=>{
audioContext.decodeAudioData(arrayBuffer, (buffer)=> {
//填充音频buffer数据
userAudioBufferSourceNode.buffer = buffer
//获取音频时长
let durationInt = setInterval(()=>{
let duration = userAudioBufferSourceNode.buffer.duration
if(duration){
console.log("获取的音频时长:",duration)
// 调用 service 层的方法
ownerInstance.callMethod('updateDuration', {
duration: duration
})
clearInterval(durationInt)
}
});
//设置音频路由节点图
userAudioBufferSourceNode
.connect(userGainNode)
.connect(dynamicsCompressorNode)
});
});
}
}
}
//base64转arrayBuffer
dataURLtoArray(base64) {
return new Promise((resolve,reject)=>{
var arr = base64.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
let blob = new Blob([u8arr], { type: mime });
blob.lastModifiedDate = new Date();
blob.name = 'fileName';
let fileReader = new FileReader;
fileReader.readAsArrayBuffer(blob);
fileReader.onload = function() {
arrayBuffer = this.result;
resolve(arrayBuffer);
}
});
},
环境:ios模拟器 iphone8
g***@live.cn (作者)
莫名其妙的好了 醉了 群主谢谢
2020-03-27 19:18