前言:vue中嵌入使用wavesurfer.js的教程详见:https://blog.csdn.net/zrcj0706/article/details/104635357
1、Minimap插件的使用效果如下图所示
2、Minimap插件的使用见如下代码
首先import引入插件依赖
import Minimap from 'wavesurfer.js/dist/plugin/wavesurfer.minimap.js'
<script>中,在WaveSurfer.create方法的plugins中定义Minimap插件
this.wavesurfer = WaveSurfer.create({
// 应该在其中绘制波形的CSS选择器或HTML元素。这是唯一必需的参数。
container: this.$refs.waveform,
plugins: [
// Minimap
Minimap.create({
waveColor: 'grey',
progressColor: 'black',
height: 30
})
]
});
3、完整代码如下
<template>
<div class="mixin-components-container">
<el-row>
<el-card class="box-card" style="text-align:left">
<div id="waveform" ref="waveform">
<!-- Here be the waveform -->
</div>
<div>
<el-button type="primary" @click="plays"> 播放/暂停</el-button>
</div>
</el-card>
</el-row>
</div>
</template>
<script>
import WaveSurfer from 'wavesurfer.js'
import Minimap from 'wavesurfer.js/dist/plugin/wavesurfer.minimap.js'
export default {
name: "Details",
// components: { MyWaveSurfer },
data() {
return {
wavesurfer: null,
};
},
mounted() {
this.$nextTick(() => {
console.log(WaveSurfer)
this.wavesurfer = WaveSurfer.create({
// 应该在其中绘制波形的CSS选择器或HTML元素。这是唯一必需的参数。
container: this.$refs.waveform,
waveColor: 'violet',
// 光标后面的波形部分的填充色。当progressColor和waveColor相同时,完全不渲染进度波
progressColor: 'purple',
backend: 'MediaElement',
// 音频播放时间轴
mediaControls: false,
// 播放音频的速度
audioRate: '1',
// 插件:此教程配置了光标插件和时间轴插件
plugins: [
// Minimap
Minimap.create({
waveColor: 'grey',
progressColor: 'black',
height: 30
})
]
});
this.wavesurfer.on('error', function(e) {
console.warn(e);
});
this.wavesurfer.load(require('./peaks/sample.mp3'));
})
},
methods: {
// 播放时暂停,播放时暂停
plays() {
this.wavesurfer.playPause()
},
}
}
</script>
<style scoped>
.mixin-components-container {
background-color: #f0f2f5;
padding: 30px;
min-height: calc(100vh - 84px);
}
</style>