1.BUG 产生及描述
页面点击录音调听按钮,ajax请求后端,后端请求原厂商服务器B拿到文件地址https://xxx.wav 返回给前端,拖拽进度条不好使。
<audio :src="src" controls="controls" ref="audio" class="audio"></audio>
2.解决方法
网上很多说法譬如:
https://blog.csdn.net/weixin_43616097/article/details/106342909,(后端设置响应头)(https://blog.csdn.net/weixin_43616097/article/details/106342909 (前端nginx添加响应头配置)
我的做法其实就是用ajax 重新请求一下设置responseType: 'blob',然后生成一个blob对象window.URL.createObjectURL 就好使了。
这里给音频播放器轻微的封装了一下,通过vux变量来控制展示(由于层级太多)(vue文件)
2.1 主文件
<el-table-column prop="callUuid" label="操作" width="60" header-align="center">
<template slot-scope="{ row }">
<el-button size="mini" type="text" @click="recoding(row)">调听</el-button>
</template>
</el-table-column>
import { mapMutations } from 'vuex'
methods: {
...mapMutations(['setIsAudio']),
function recoding (row) {
// 调用接口返回音频地址
this.setIsAudio(false) // 设置vux
let url = 'yyy?id=' // 后端地址
this.axios.get(url + row.id).then(data => {
if (data) {
this.setIsAudio(data)
} else {
this.$message({ message: '未找到录音!', type: 'error' })
}
})
}
}
2.2 音频组件
<template>
<div v-show="isShow" class="video-dialog" v-drag="drag">
<audio :src="audioFile" controls="controls" ref="audio" class="audio"></audio>
<div class="btn">
<div class="close-area" @click="close">
<i class="el-icon-close"></i>
</div>
<div class="drag-area" @mousedown="enableDrag" @mouseleave="disableDrag">
<i class="el-icon-rank"></i>
</div>
</div>
</div>
</template>
<script>
import {mapState} from 'vuex'
export default {
name: 'VideoCall',
data () {
return {
drag: false,
isShow: false,
audioFile: ''
}
},
computed: {
...mapState(['isAudio'])
},
watch: {
isAudio: {
handler (val) {
if (val) {
this.showDialog(val)
} else {
this.audioFile = ''
}
},
deep: true
}
},
methods: {
async showDialog (val) {
const v = await this.getVideoData(val)
if (v === true) {
this.isShow = true
} else {
this.audioFile = ''
}
},
getVideoData (val) {
return new Promise((resolve, reject) => {
this.axios({
method: 'get',
responseType: 'blob',
url: val
}).then((res) => {
console.log('111111111111111', res)
this.audioFile = window.URL.createObjectURL(res.data)
resolve(true)
})
})
},
close () {
this.isShow = false
this.audioFile = ''
},
enableDrag () {
this.drag = true
},
disableDrag () {
this.drag = false
}
}
}
.video-dialog {
position: fixed;
width: 400px;
height: 50px;
border-radius: 30px;
box-shadow: 0px 3px 5px 0px #adadad;
z-index: 2000;
background-color: #f0f2f3;
top: 78px;
right: 20px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: flex-start;
.audio{
width: 300px;
height: 54px;
}
.btn{
width: 40px;
border-left: 1px dotted #1764ce;
font-size: 20px;
color: #1764ce;
line-height: 20px;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: center;
padding-left: 5px;
.close-area{
cursor: pointer;
}
.drag-area {
cursor: move;
}
}