效果:保存录音,存入vuex仓库中,再次进入调取store中的音频文件,展示在聊天界面上
问题:存储到了音频文件,却没有找到播放插件中的使用该音频的方式,
解决:在看了audio标签后发现链接是src导入音频,但是直接导入失败,我认为这个导入失败是因为文件格式不一致,如何确定这个文件格式,必须要去看这个播放插件的js的内容,这是一个可以解决诸多问题的一种方法,以后要经常使用,通过这次成功也算是一种自我突破,我发现了他的play方法中对音频文件进行了src的适配调整,window.URL.createObjectURL()
插件:import Record from ‘../../views/social/recorder-sdk’;
fuyanyuyin(){
var d =this.$store.state.yuyins
if (d.length>0) {
for (let u = 0; u < d.length; u++) {
this.vnumber++
if (this.vnumber>0) {
var p =window.URL.createObjectURL(d[u])
console.log(p);
var $editor = $(".J__wcEditor"), _editor = $editor[0];
var $chatMsgList = $("#J__chatMsgList");
var html = $editor.html();
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
html = html.replace(reg, "<a href='$1$2'>$1$2</a>");
var msgTpl = [
'<li class="others">\
<a class="avatar"><img src="https://img0.baidu.com/it/u=3320986191,3417780337&fm=26&fmt=auto&gp=0.jpg" /></a>\
<div class="content">\
<p class="author">志愿者</p>\
<div class="record-play">\
<audio src="'+p+'"\
id="audioVoice'+this.videonumber+'"\
controls\
preload="auto"></audio>\
</div>\
</div>\
</li>'
].join("");
$chatMsgList.append(msgTpl);
$(".wc__chatMsg-panel").animate({scrollTop: $("#J__chatMsgList").height()}, 0);
}
}
}
},
发送录音
// 开始录音
onStartVoice () {
// this.onStopAudio()
this.isFinished = false;
this.recorder.startRecord({
success: res => {
this.isVoice = true
},
error: e => {
this.isVoice = false
this.$toast(e)
}
});
},
// 结束录音
onEndVoice () {
this.isFinished = false;
this.recorder.stopRecord({
success: res => {
this.isVoice = false
//此处可以获取音频源文件(res),用于上传等操作
console.log('音频源文件', res)
this.$store.commit('yuyinsnum',res)
},
error: e => {
this.isVoice = false
}
});
},
// 播放录音
onPlayAudio () {
this.videonumber++
if (this.videonumber>0) {
var $editor = $(".J__wcEditor"), _editor = $editor[0];
var $chatMsgList = $("#J__chatMsgList");
var html = $editor.html();
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
html = html.replace(reg, "<a href='$1$2'>$1$2</a>");
var nowtime =dayjs().format('YYYY-MM-DD HH:mm:ss')
var msgTpl = [
'<li class="time"><span>'+nowtime+'</span></li>\
<li class="me" >\
<div class="content">\
<p class="author">志愿者</p>\
<div class="record-play">\
<audio id="audioVoice'+this.videonumber+'"\
controls\
autoplay></audio>\
</div>\
</div>\
<a class="avatar" ><img src="https://img0.baidu.com/it/u=3320986191,3417780337&fm=26&fmt=auto&gp=0.jpg" /></a>\
</li>'
].join("");
$chatMsgList.append(msgTpl);
$(".wc__chatMsg-panel").animate({scrollTop: $("#J__chatMsgList").height()}, 0);
this.isVoice = false
this.isFinished = true;
var VideoId ='audioVoice'+this.videonumber
this.audio = document.getElementById(VideoId);
this.recorder.play(this.audio);
}
},
// 停止播放录音
onStopAudio () {
this.recorder.clear(this.audio);
}
},