当前位置:   article > 正文

声纹可视化工具:wavesurfer.js---在Vue中使用音频声纹可视化插件wavesurfer.js【已封装成组件有完整demo实例附完整代码】

wavesurfer

效果图:

完整代码在最后。

使用方法:

1、第一步:使用以下命令安装wavesurfer.js插件库

npm install wavesurfer.js --save

2、第二步:在需要使用的页面import导入wavesurfer.js和其中的时间轴timeline插件

  1. import WaveSurfer from 'wavesurfer.js'
  2. import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.js'
  3. import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js'

3、第三步:在<template>里面加入3个主要部分:声纹图案+语音刻度+控制按钮

4、第四步:在<script>里面加入3个主要部分:在data里面预设一些初始值+在mounted里面绘图并监听页面宽度变化后重新加载绘图+在methods里面写入需要使用到的公用方法

 
 

官方文档:

wavesurfer.js

参数:

wavesurfer.js

常用方法:

wavesurfer.js

事件:

wavesurfer.js

api:

Home | wavesurfer.js

参考文章:

Vue中整合使用wavesurfer.js声纹可视化插件_只如初见0706的博客-CSDN博客_wavesurfer

vue 中使用wavesurfer.js绘制音频图案_淘淘是只狗的博客-CSDN博客_vue 音频波形图

浅析wavesurfer.js中各大方法的使用_只如初见0706的博客-CSDN博客_wavesurfer

在Vue中使用音频可视化插件wavesurfer.js - 掘金

还有个wavesurfer-vue还没研究:

Vue Wrapper for wavesurfer.js

完整代码:

  1. <template>
  2. <div id="mixin-components-container" ref="myWave">
  3. <el-row>
  4. <el-card class="box-card" style="text-align:left">
  5. <div id="waveform" ref="waveform" v-loading="loading">
  6. <!-- Here be the waveform -->
  7. </div>
  8. <div id="wave-timeline" ref="wave-timeline">
  9. <!--时间轴 -->
  10. </div>
  11. <div>
  12. <el-row>
  13. <el-col :span="2">
  14. <div class="grid-content bg-purple-dark">
  15. <el-button type="primary" @click="rew">后退</el-button>
  16. </div>
  17. </el-col>
  18. <el-col :span="4">
  19. <div class="grid-content bg-purple-dark">
  20. <el-button type="primary" @click="plays">
  21. <i class="el-icon-video-play"></i>
  22. 播放 /
  23. <i class="el-icon-video-pausee"></i>
  24. 暂停
  25. </el-button>
  26. </div>
  27. </el-col>
  28. <el-col :span="2">
  29. <div class="grid-content bg-purple-dark">
  30. <el-button type="primary" @click="forward">前进</el-button>
  31. </div>
  32. </el-col>
  33. <el-col :span="2">
  34. <div class="grid-content bg-purple-dark">
  35. <el-button type="primary" @click="replay">重放</el-button>
  36. </div>
  37. </el-col>
  38. <el-col :span="3">
  39. <div class="grid-content bg-purple-dark">
  40. <a :href="voiceSrc" target="_blank">
  41. <el-button type="primary" class="down-btn">下载文件</el-button>
  42. </a>
  43. </div>
  44. </el-col>
  45. <el-col :span="5">
  46. <div class="grid-content bg-purple-dark">
  47. </div>
  48. </el-col>
  49. <el-col :span="2">
  50. <div class="grid-content bg-purple-dark">
  51. <el-tooltip class="item" effect="dark" content="指定播放" placement="bottom">
  52. <el-popover placement="top" width="200" trigger="click">
  53. <el-input v-model="appointTime" placeholder="请输入内容" class="input-with-select">
  54. <el-button slot="append" @click="appointPlay">播放</el-button>
  55. </el-input>
  56. <el-button slot="reference" circle>
  57. 指定播放
  58. </el-button>
  59. </el-popover>
  60. </el-tooltip>
  61. </div>
  62. </el-col>
  63. <el-col :span="2">
  64. <div class="grid-content bg-purple-dark">
  65. <el-tooltip class="item" effect="dark" content="音量" placement="bottom">
  66. <el-popover placement="top-start" trigger="click" style="min-width: 38px;">
  67. <div class="block" style="width: 42px">
  68. <el-slider v-model="volumeValue" vertical height="100px" @change="setVolume(volumeValue)"/>
  69. </div>
  70. <el-button slot="reference" circle>
  71. 音量
  72. </el-button>
  73. </el-popover>
  74. </el-tooltip>
  75. </div>
  76. </el-col>
  77. <el-col :span="2">
  78. <div class="grid-content bg-purple-dark">
  79. <el-tooltip class="item" effect="dark" content="播放倍速" placement="bottom">
  80. <el-popover placement="top" width="220" trigger="click" style="margin-left: 10px">
  81. <el-input-number v-model="ds" width="180" :precision="2" :step="0.1" :min="0.5" :max="2"
  82. @change="DoubleSpeed(ds)"
  83. />
  84. <el-button slot="reference" round>
  85. {{ ds + ' X' }}
  86. </el-button>
  87. </el-popover>
  88. </el-tooltip>
  89. </div>
  90. </el-col>
  91. </el-row>
  92. </div>
  93. </el-card>
  94. </el-row>
  95. </div>
  96. </template>
  97. <script>
  98. import WaveSurfer from 'wavesurfer.js'
  99. import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.js'
  100. import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js'
  101. import elementResizeDetectorMaker from 'element-resize-detector'
  102. export default {
  103. name: 'myWaveSurfer',
  104. props: ['voiceSrc'],
  105. data() {
  106. return {
  107. // isPlaying: false,
  108. // time: '00:00',
  109. // wavesurfer: null,
  110. wavesurfer: null,
  111. // 指定播放功能的播放时间点
  112. appointTime: 1,
  113. // 播放倍速
  114. ds: 1.00,
  115. // 设置音量
  116. volumeValue: [1],//将播放音量设置为新值[0..1](0 =静音,1 =最大)
  117. screenWidth: document.body.clientWidth,
  118. loading: false
  119. }
  120. },
  121. watch: {
  122. screenWidth() {
  123. // 页面宽度变化时。重绘需要加载动画
  124. this.loading = true
  125. }
  126. },
  127. mounted() {
  128. this.$nextTick(() => {
  129. // console.log('加载WaveSurfer')
  130. // console.log(WaveSurfer)
  131. this.wavesurfer = WaveSurfer.create({
  132. // 应该在其中绘制波形的CSS选择器或HTML元素。这是唯一必需的参数。
  133. container: this.$refs.waveform,
  134. // 光标的填充颜色,指示播放头的位置。
  135. cursorColor: 'red',
  136. // 更改波形容器的背景颜色。
  137. backgroundColor: 'white',
  138. // 光标后的波形填充颜色。
  139. waveColor: '#0066CC',
  140. // 光标后面的波形部分的填充色。当progressColor和waveColor相同时,完全不渲染进度波
  141. progressColor: '#99CCFF',
  142. backend: 'MediaElement',
  143. // 音频播放时间轴
  144. mediaControls: false,
  145. // 播放音频的速度
  146. audioRate: '1',
  147. // 插件:此教程配置了光标插件和时间轴插件
  148. plugins: [
  149. // 光标插件
  150. CursorPlugin.create({
  151. showTime: true,
  152. opacity: 1,
  153. customShowTimeStyle: {
  154. 'background-color': '#000',
  155. color: '#fff',
  156. padding: '2px',
  157. 'font-size': '10px'
  158. }
  159. }),
  160. // 时间轴插件
  161. Timeline.create({
  162. container: '#wave-timeline'
  163. })
  164. ]
  165. })
  166. this.wavesurfer.on('error', function(e) {
  167. console.warn(e)
  168. })
  169. //加载音频url
  170. this.wavesurfer.load(this.voiceSrc)
  171. this.volumeValue = this.wavesurfer.getVolume() * 100
  172. })
  173. let that = this
  174. window.onresize = () => {
  175. return (() => {
  176. window.screenWidth = document.body.clientWidth
  177. that.screenWidth = window.screenWidth
  178. })()
  179. }
  180. let erd = elementResizeDetectorMaker()
  181. erd.listenTo(document.getElementById('mixin-components-container'), (element) => {
  182. that.$nextTick(() => {
  183. that.wavesurfer.load(this.voiceSrc)
  184. setTimeout(() => {
  185. that.loading = false
  186. }, 2000)
  187. })
  188. })
  189. },
  190. methods: {
  191. // 播放时暂停,播放时暂停
  192. plays() {
  193. this.wavesurfer.playPause()
  194. },
  195. // 后退,
  196. rew() {
  197. this.wavesurfer.skip(-3)
  198. },
  199. // 前进,
  200. forward() {
  201. this.wavesurfer.skip(3)
  202. },
  203. // 重放
  204. replay() {
  205. this.wavesurfer.stop()
  206. },
  207. // 设置音量:
  208. setVolume(val) {
  209. this.wavesurfer.setVolume(val / 100)
  210. console.log(val)
  211. // getVolume() –返回当前音频片段的音量。
  212. console.log(this.wavesurfer.getVolume())
  213. },
  214. // 指定播放
  215. appointPlay() {
  216. this.wavesurfer.play([this.appointTime])
  217. },
  218. //倍速播放
  219. DoubleSpeed(rate) {
  220. this.wavesurfer.setPlaybackRate(rate)
  221. console.log(rate)
  222. }
  223. }
  224. }
  225. </script>
  226. <style lang="scss" scoped>
  227. #mixin-components-container {
  228. background-color: lightseagreen;
  229. //padding: 30px;
  230. min-height: 221px;
  231. width: 100%;
  232. border-radius: 10px;
  233. //公共el-row
  234. .el-row {
  235. margin-bottom: 20px;
  236. text-align: center;
  237. &:last-child {
  238. margin-bottom: 0;
  239. }
  240. }
  241. .el-col {
  242. border-radius: 4px;
  243. }
  244. .bg-purple-dark {
  245. background: white;
  246. }
  247. .bg-purple {
  248. background: #d3dce6;
  249. }
  250. .bg-purple-light {
  251. background: #e5e9f2;
  252. }
  253. .grid-content {
  254. border-radius: 4px;
  255. min-height: 36px;
  256. }
  257. .row-bg {
  258. padding: 10px 0;
  259. background-color: #f9fafc;
  260. }
  261. .box-card {
  262. border: none;
  263. #waveform {
  264. }
  265. #wave-timeline {
  266. margin: 20px auto;
  267. background-color: lightgray;
  268. }
  269. }
  270. }
  271. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/130173
推荐阅读
相关标签
  

闽ICP备14008679号