赞
踩
安装 wavesurfer.js
cnpm i wavesurfer.js --save
无 cnpm 的先运行 npm i cnpm -g 全局安装 cnpm
绘制音频波形图
常用的配置参数详见注释。
<template>
<div style="padding: 30px">
<div ref="waveform_Ref"></div>
<div style="padding: 30px">
<el-button
type="primary"
size="small"
icon="el-icon-video-play"
@click="playMusic"
v-if="!playing"
>
播放 </el-button
><el-button
v-if="playing"
type="primary"
size="small"
icon="el-icon-video-pause"
@click="playMusic"
>
暂停
</el-button>
</div>
</div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
export default {
data() {
return {
wavesurfer: null,
playing: false,
};
},
mounted() {
this.$nextTick(() => {
this.wavesurfer = WaveSurfer.create({
// 波形图的容器
container: this.$refs.waveform_Ref,
// 已播放波形的颜色
// progressColor: "red",
// 未播放波形的颜色
// waveColor: "lightgrey",
// 波形图的高度,单位为px
// height: 10,
// 是否显示滚动条,默认为false
// scrollParent: true,
// 波形的振幅(高度),默认为1
// barHeight: 0.8,
// 波形条的圆角
// barRadius: 2,
// 波形条的宽度
// barWidth: 1,
// 波形条间的间距
// barGap: 3
// 播放进度光标条的颜色
// cursorColor: "red",
// 播放进度光标条的宽度,默认为1
// cursorWidth: 10,
// 播放进度颜色
// progressColor: "blue",
// 波形容器的背景颜色
// backgroundColor: "yellow",
// 音频的播放速度
// audioRate: "1",
// (与区域插件一起使用)启用所选区域的循环
// loopSelection:false
});
this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
});
},
methods: {
playMusic() {
this.wavesurfer.playPause.bind(this.wavesurfer)();
this.playing = !this.playing;
},
},
};
</script>
<style scoped>
</style>
常用方法
this.wavesurfer.playPause();
this.wavesurfer.stop();
// 从当前位置向后快进2秒播放
this.wavesurfer.skip(2);
// 从当前位置向后快退2秒播放
this.wavesurfer.skip(-2);
参数值为0到1
– 0为静音
– 0.5为一半音量
– 1为最高音量
this.wavesurfer.setVolume(0.5);
默认为1
this.wavesurfer.setPlaybackRate(2)
参数是每秒音频的水平像素数。它还会更改参数minPxPerSec并启用该 scrollParent选项。
this.wavesurfer.zoom(88)
从第10秒开始播放
this.wavesurfer.play(10);
播放第1-10秒之间的音频
this.wavesurfer.play(1, 10);
this.wavesurfer.getCurrentTime()
this.wavesurfer.getDuration();
创建播放实例后调用
常用事件
使用on()和un() 方法订阅和取消订阅各种播放器事件
this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
// 自动播放
let that = this;
this.wavesurfer.on("ready", function () {
that.wavesurfer.play();
});
谷歌浏览器默认禁止音频自动播放,解除此设置的方法为:
https://blog.csdn.net/weixin_41192489/article/details/127671295
插件–时间轴
<template>
<div style="padding: 30px">
<div ref="waveform_Ref"></div>
<div ref="timeline_Ref"></div>
<div style="padding: 30px">
<el-button
type="primary"
size="small"
icon="el-icon-video-play"
@click="playMusic"
v-if="!playing"
>
播放 </el-button
><el-button
v-if="playing"
type="primary"
size="small"
icon="el-icon-video-pause"
@click="playMusic"
>
暂停
</el-button>
</div>
</div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
//导入插件--时间轴
import Timeline from "wavesurfer.js/dist/plugin/wavesurfer.timeline.js";
export default {
data() {
return {
wavesurfer: null,
playing: false,
};
},
mounted() {
this.$nextTick(() => {
this.wavesurfer = WaveSurfer.create({
container: this.$refs.waveform_Ref,
plugins: [
//插件--时间轴的配置
Timeline.create({
container: this.$refs.timeline_Ref,
fontSize: 14,
//主要时间标签颜色
primaryColor: "#9191a5",
//主要时间文字颜色
primaryFontColor: "#9191a5",
//次要时间标签颜色
secondaryColor: "#9191a5",
//次要时间文字颜色
secondaryFontColor: "#9191a5",
}),
],
});
this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
});
},
methods: {
playMusic() {
this.wavesurfer.playPause();
this.playing = !this.playing;
},
},
};
</script>
插件–指针轴
<template>
<div style="padding: 30px">
<div ref="waveform_Ref"></div>
</div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
// 导入插件--指针轴
import CursorPlugin from "wavesurfer.js/dist/plugin/wavesurfer.cursor.js";
export default {
data() {
return {
wavesurfer: null,
};
},
mounted() {
this.$nextTick(() => {
this.wavesurfer = WaveSurfer.create({
container: this.$refs.waveform_Ref,
plugins: [
// 插件--指针轴的配置
CursorPlugin.create({
showTime: true,
opacity: 1,
customShowTimeStyle: {
"background-color": "#000",
color: "#fff",
padding: "2px",
"font-size": "10px",
},
}),
],
});
this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
});
},
};
</script>
插件–标注
<template>
<div style="padding: 30px">
<div ref="waveform_Ref"></div>
</div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
// 导入插件--标注
import markers from "wavesurfer.js/dist/plugin/wavesurfer.markers.js";
export default {
data() {
return {
wavesurfer: null,
};
},
mounted() {
this.$nextTick(() => {
this.wavesurfer = WaveSurfer.create({
container: this.$refs.waveform_Ref,
plugins: [
// 插件--标注的配置
markers.create({
markers: [
{
time: 15,
label: "第15秒",
color: "red",
},
{
time: 40,
label: "第40秒",
color: "blue",
position: "top",
},
],
}),
],
});
this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
});
},
};
</script>
this.wavesurfer.addMarker({
time: 60,
label: "第60秒",
color: "blue",
position: "top",
});
插件–区域
官方文档 https://wavesurfer-js.org/plugins/regions.html
<template>
<div style="padding: 30px">
<div ref="waveform_Ref"></div>
</div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
// 导入插件--区域
import RegionsPlugin from "wavesurfer.js/dist/plugin/wavesurfer.regions.min.js";
export default {
data() {
return {
wavesurfer: null,
};
},
mounted() {
this.$nextTick(() => {
this.wavesurfer = WaveSurfer.create({
container: this.$refs.waveform_Ref,
plugins: [
// 插件--区域的配置
RegionsPlugin.create({
regionsMinLength: 2,
regions: [
{
start: 10,
end: 30,
loop: false,
color: "hsla(400, 100%, 30%, 0.5)",
},
{
start: 50,
end: 70,
loop: false,
color: "hsla(200, 50%, 70%, 0.4)",
minLength: 1,
maxLength: 5,
},
],
dragSelection: {
slop: 5,
},
}),
],
});
this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
});
},
};
</script>
this.wavesurfer.addRegion({
start: 2, // 区域开始时间
end: 10,// 区域结束时间
color: "red",// 区域颜色
drag: false,// 是否可拖拽
loop: false,// 是否循环播放
resize: false,// 是否可改变大小
minLength: 1, // 区域最小值
maxLength: 5, // 区域最大值
});
this.wavesurfer.clearRegions()
绘制视频波形图
https://wavesurfer-js.org/example/video-element/index.html
绘制渐变色的炫彩波形图
https://wavesurfer-js.org/example/gradient-fill-styles/index.html
改变左右声道
https://wavesurfer-js.org/example/panner/index.html
更多功能
详见官网文档 https://wavesurfer-js.org/docs/
实战范例
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。