当前位置:   article > 正文

vue音频wavesurfer波形图_vue 声音波浪

vue 声音波浪

1.效果图

在这里插入图片描述

2.npm install wavesurfer.js

3.main.js或者使用组件内引入

	import WaveSurfer from 'wavesurfer.js'
	import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.js'
	import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js'
  • 1
  • 2
  • 3
<template>
	<div class="mixin-components-container" style="width: 100%;">
		<div class="box-card" style="width:100%;text-align:left;background: #43444c;height:210px;">
			<div id="wave-timeline" ref="wave-timeline" style="margin-top: 20px;">
				<!--时间轴 -->
			</div>
			<div id="waveform" ref="waveform">
				<!-- Here be the waveform -->
			</div>

			<div style="margin-top: 10px;padding-bottom: 10px;">
				<el-select v-model="ds" placeholder="请选择播放速度" @change="DoubleSpeed"
					style="float: left;background: #363740;color:#bfc0dc;margin-left: 10px;">
					<el-option v-for="item in audiominmax" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
				<!-- <img src="../../assets/img/audio1.png" alt="" title="音量"
					style="float: left;margin-left: 30px;margin-right: 14px;margin-top: 9px;cursor: pointer;width: 20px;height:20px;"> -->
				<i class="el-icon-microphone" title="音量"
					style="float: left;margin-left: 30px;margin-right: 14px;margin-top: 5px;cursor: pointer;font-size:24px;color:#bfc0dd;"></i>
				<el-slider v-model="value" @change="setVolume" input-size="mini"
					style="width:100px;float: left;margin-right:30px;cursor: pointer;" />
				<i class="el-icon-d-arrow-left" @click="rew" alt="" title="快退"
					style="float: left;margin-top: 11px;margin-left: 10px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>
				<i class="el-icon-arrow-left"  title="上一个"
					style="float: left;margin-top: 11px;margin-left: 10px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>	
					
				<i class="el-icon-video-play"  v-if="audioplay" @click="plays" alt="" title="播放"
					style="float: left;margin-top: 11px;margin-left: 4px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>
				<i class="el-icon-video-pause"  v-if="!audioplay" @click="plays" alt="" title="暂停"
					style="float: left;margin-top: 11px;margin-left: 4px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>
					
				<i class="el-icon-arrow-right"  title="下一个"
					style="float: left;margin-top: 11px;margin-left: 10px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>	
				<i class="el-icon-d-arrow-right" @click="speek" alt="" title="快进"
					style="float: left;margin-top: 11px;margin-left: 6px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>
				<i class="el-icon-refresh" @click="replay" alt="" title="重放"
					style="float: left;margin-top: 11px;margin-left: 20px;cursor: pointer;font-size:20px;color:#bfc0dd;"></i>
				<!-- <img src="../../assets/img/audio2.png" @click="rew" alt="" title="快退"
					style="float: left;margin-top: 11px;margin-left: 10px;cursor: pointer;width: 20px;height:20px;">
				<img src="../../assets/img/audio5.png" v-if="audioplay" @click="plays" alt="" title="播放"
					style="float: left;margin-top: 11px;margin-left: 4px;cursor: pointer;width: 20px;height:20px;">
				<img src="../../assets/img/audio3.png" v-if="!audioplay" @click="plays" alt="" title="播放"
					style="float: left;margin-top: 11px;margin-left: 4px;cursor: pointer;width: 20px;height:20px;">

				<img src="../../assets/img/audio4.png" @click="speek" alt="" title="快进"
					style="float: left;margin-top: 11px;margin-left: 4px;cursor: pointer;width: 20px;height:20px;">

				<img src="../../assets/img/audio6.png" @click="replay" alt="" title="重放"
					style="float: left;margin-top: 11px;margin-left: 20px;cursor: pointer;width: 20px;height:20px;"> -->
				<!-- <el-button type="primary" @click="rew">后退</el-button>
          <el-button type="primary" @click="plays">
            <i class="el-icon-video-play"></i>
            播放 /
            <i class="el-icon-video-pausee"></i>
            暂停
          </el-button>
           <el-button type="primary" @click="speek">前进</el-button>
           <el-button type="primary" @click="replay">重放</el-button>
             <el-tooltip class="item" effect="dark" content="指定播放" placement="bottom">
              <el-popover placement="top" width="200" trigger="click">
                <el-input v-model="appointTime" placeholder="请输入内容" class="input-with-select">
                  <el-button slot="append" @click="appointPlay">播放</el-button>
                </el-input>
                <el-button slot="reference" circle>
                  指定播放
                </el-button>
              </el-popover>
            </el-tooltip>
             <span style="border: 2px solid #2f4f4f;margin-left: 8px;margin-right: 4px" /> -->
				<!-- <el-tooltip class="item" effect="dark" content="音量" placement="bottom">
              <el-popover placement="top-start" trigger="click" style="min-width: 38px;margin-left: 10px">
                <div class="block" style="width: 42px">
                  <el-slider v-model="value" vertical  @change="setVolume" />
                </div>
                <el-button slot="reference" circle>
                  音量
                </el-button>
              </el-popover>
            </el-tooltip> -->
			</div>

		</div>
	</div>
</template>
<script>
	import WaveSurfer from 'wavesurfer.js'
	import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.js'
	import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js'
	export default {
		name: "Details",
		// components: { MyWaveSurfer },
		data() {
			return {
				audiominmax: [{
					value: 1.50,
					label: '播放速度 150%'
				}, {
					value: 1.20,
					label: '播放速度 120%'
				}, {
					value: 1.00,
					label: '播放速度 100%'
				}, {
					value: 0.50,
					label: '播放速度 50%'
				}],
				wavesurfer: null,
				// 指定播放功能的播放时间点
				appointTime: 1,
				// 播放倍速
				ds: 1.00,
				// 设置音量
				value: 30,
				audioplay: true,
			};
		},
		beforeDestroy(){
			this.wavesurfer.destroy();
		},
		mounted() {
			let that=this;
			this.$nextTick(() => {
				console.log(WaveSurfer)
				this.wavesurfer = WaveSurfer.create({
					// 应该在其中绘制波形的CSS选择器或HTML元素。这是唯一必需的参数。
					container: this.$refs.waveform,
					// 更改波形容器的背景颜色。
					backgroundColor: '#000000',
					backend: 'MediaElement',

					//走动指针线颜色
					cursorColor: "red",
					//频率背景颜色
					waveColor: '#42d995',
					//走动颜色
					progressColor: "#42d995",
					// 音频播放时间轴
					mediaControls: false,
					// 播放音频的速度
					audioRate: '1',
					// 插件:此教程配置了光标插件和时间轴插件
					plugins: [
						// 光标插件
						CursorPlugin.create({
							showTime: true,
							opacity: 0,
							customShowTimeStyle: {
								'background-color': '#000',
								color: '#fff',
								padding: '2px',
								'font-size': '10px'
							}
						}),
						// 时间轴插件
						Timeline.create({
							container: '#wave-timeline'
						}),
					]
				});
				this.wavesurfer.on('ready', function(e) {
					console.warn('加载成功---',e);
					that.plays();
					that.wavesurfer.setVolume(30 / 100)
				});
				this.wavesurfer.on('finish', function(e) {
					console.warn('播放结束---',e);
					// that.plays();
					that.audioplay = true;
				});
				this.wavesurfer.on('error', function(e) {
					console.warn(e);
				});
				this.wavesurfer.load(require("../../mp3/tizzy.mp3"));
				
				// this.value = this.wavesurfer.getVolume() * 100
			})
		},
		methods: {
			// 播放时暂停,播放时暂停
			plays() {
				this.audioplay = !this.audioplay;
				this.wavesurfer.playPause()
			},
			// 后退,
			rew() {
				this.wavesurfer.skip(-3)
			},
			// 前进,
			speek() {
				this.wavesurfer.skip(3)
			},
			// 重放
			replay() {
				this.wavesurfer.stop()
			},
			// 设置音量:
			setVolume(val) {
				this.wavesurfer.setVolume(val / 100)
			},
			// 指定播放
			appointPlay() {
				this.wavesurfer.play([this.appointTime, ])
			},
			//倍速播放
			DoubleSpeed(rate) {
				this.wavesurfer.setPlaybackRate(rate)
				console.log(rate)
			}

		}
	}
</script>
<style scoped>
	/deep/ .el-input__inner {
		background-color: #363740;
		border: 1px solid #797979;
		color: #aeafc8;
	}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/130203
推荐阅读