Vue框架中嵌入使用wavesurfer._vue使用wavesurfer的光标能够拖动">
当前位置:   article > 正文

2021/11/26 wavesurfer的使用 及插件的使用_vue使用wavesurfer的光标能够拖动

vue使用wavesurfer的光标能够拖动

效果图

完整代码如下

  1. <template>
  2. <div class="mixin-components-container">
  3. <el-row>
  4. <el-card class="box-card" style="text-align: left">
  5. <div slot="header" class="clearfix title">
  6. <span>Vue框架中嵌入使用wavesurfer.js插件</span>
  7. </div>
  8. <div id="waveform" ref="waveform">
  9. <!-- Here be the waveform -->
  10. </div>
  11. <div id="wave-timeline" ref="wave-timeline">
  12. <!--时间轴 -->
  13. </div>
  14. <el-row class="row">
  15. <!-- 播放/暂停按钮 -->
  16. <el-col :span="2">
  17. <el-button type="primary" @click="playMusic">
  18. <i class="el-icon-video-play"></i>
  19. 播放 /
  20. <i class="el-icon-video-pausee"></i>
  21. 暂停
  22. </el-button>
  23. </el-col>
  24. <!-- 前进 -->
  25. <el-col :span="2">
  26. <div class="grid-content bg-purple-dark">
  27. <el-button class="allbtn normal primary" @click="forward">
  28. Forward
  29. </el-button>
  30. </div>
  31. </el-col>
  32. <!-- 选中区域播放/暂停按钮 -->
  33. <el-col :span="3">
  34. <el-button type="primary" @click="play">
  35. <i class="el-icon-video-play"></i>
  36. 播放 选中区域
  37. </el-button>
  38. </el-col>
  39. <el-col :span="0.7">
  40. <i class="el-icon-zoom-in"></i>
  41. </el-col>
  42. <!-- 波形缩放 -->
  43. <el-col :span="4">
  44. <input
  45. data-action="zoom"
  46. @change="zoom(zooms)"
  47. v-model="zooms"
  48. type="range"
  49. min="20"
  50. max="1000"
  51. value="0"
  52. style="width: 98%"
  53. />
  54. </el-col>
  55. <el-col :span="0.7">
  56. <i class="el-icon-zoom-out"></i>
  57. </el-col>
  58. <!-- 音量 -->
  59. <el-col :span="2" style="margin-left: 25px">
  60. <div class="grid-content bg-purple-dark">
  61. <el-popover
  62. placement="top-start"
  63. trigger="click"
  64. width="45"
  65. min-width="45"
  66. style="min-width: 38px"
  67. >
  68. <div class="block" style="width: 42px">
  69. <el-slider
  70. v-model="volumeValue"
  71. vertical
  72. height="100px"
  73. @change="setVolume(volumeValue)"
  74. />
  75. </div>
  76. <el-button class="normal allbtn primary" slot="reference">
  77. Volume
  78. </el-button>
  79. </el-popover>
  80. </div>
  81. </el-col>
  82. <!-- 倍速播放 -->
  83. <el-col :span="2">
  84. <div class="grid-content bg-purple-dark">
  85. <el-tooltip
  86. class="item"
  87. effect="dark"
  88. content="Play speed"
  89. placement="bottom"
  90. >
  91. <el-popover
  92. placement="top"
  93. width="180"
  94. trigger="click"
  95. style="margin-left: 10px"
  96. >
  97. <el-input-number
  98. v-model="speed"
  99. width="180"
  100. :precision="2"
  101. :step="0.5"
  102. :min="0.5"
  103. :max="2"
  104. @change="DoubleSpeed(speed)"
  105. />
  106. <el-button slot="reference" round>
  107. {{ speed + " X" }}
  108. </el-button>
  109. </el-popover>
  110. </el-tooltip>
  111. </div>
  112. </el-col>
  113. </el-row>
  114. </el-card>
  115. </el-row>
  116. </div>
  117. </template>
  118. <script>
  119. import WaveSurfer from "wavesurfer.js";
  120. import Timeline from "wavesurfer.js/dist/plugin/wavesurfer.timeline.js";
  121. import Regions from "wavesurfer.js/dist/plugin/wavesurfer.regions.js";
  122. import CursorPlugin from "wavesurfer.js/dist/plugin/wavesurfer.cursor.js";
  123. export default {
  124. name: "Details",
  125. data() {
  126. return {
  127. voiceSrc:"",
  128. wavesurfer: null,
  129. zooms: 100, //缩放
  130. volumeValue: [1], //音频音量
  131. speed: 1.0, //倍速
  132. screenWidth: document.body.clientWidth,
  133. duration: 336, //音频时长 s
  134. //选中区域
  135. regions: {
  136. start: 0,
  137. end: 0,
  138. loop: true,
  139. showTooltip: true,
  140. },
  141. curRegion: null,
  142. };
  143. },
  144. mounted() {
  145. this.$nextTick(() => {
  146. console.log(WaveSurfer);
  147. this.wavesurfer = WaveSurfer.create({
  148. container: this.$refs.waveform,
  149. //是否波形图滚动
  150. scrollParent: true,
  151. //是否隐藏水平滚动条
  152. hideScrollbar: false,
  153. //波形显示为柱形
  154. barWidth: 5,
  155. //波形颜色
  156. waveColor: "#409EFF",
  157. progressColor: "blue",
  158. backend: "MediaElement",
  159. mediaControls: false,
  160. audioRate: "1",
  161. //使用时间轴插件
  162. plugins: [
  163. Timeline.create({
  164. container: "#wave-timeline",
  165. }),
  166. // 选中区域
  167. Regions.create({
  168. showTime: true,
  169. regions: [
  170. {
  171. start: 10, //选中区域开始时间
  172. end: 20, //选中区域开始时间
  173. attributes: {
  174. label:
  175. "选中区域开始时间1",
  176. },
  177. data: {
  178. note: "选中区域开始时间1",
  179. },
  180. loop: false, //播放时是否循环播放片段
  181. drag: true, //允许/禁止拖动区域
  182. resize: true, //允许/禁止调整区域大小
  183. color: " rgba(181, 198, 241, 0.5)", //选中区块颜色
  184. },
  185. {
  186. start: 30, //选中区域开始时间
  187. end: 45, //选中区域开始时间
  188. attributes: {
  189. label: "选中区域开始时间2",
  190. },
  191. data: {
  192. note: "选中区域开始时间2",
  193. },
  194. loop: false, //播放时是否循环播放片段
  195. drag: true, //允许/禁止拖动区域
  196. resize: true, //允许/禁止调整区域大小
  197. color: " rgba(181, 198, 241, 0.5)", //选中区块颜色
  198. },
  199. {
  200. start: 60, //选中区域开始时间
  201. end: 75, //选中区域开始时间
  202. attributes: {
  203. label: "选中区域开始时间3",
  204. },
  205. data: {
  206. note: "选中区域开始时间3",
  207. },
  208. loop: false, //播放时是否循环播放片段
  209. drag: true, //允许/禁止拖动区域
  210. resize: true, //允许/禁止调整区域大小
  211. color: " rgba(181, 198, 241, 0.5)", //选中区块颜色
  212. },
  213. ],
  214. }),
  215. // 光标插件
  216. CursorPlugin.create({
  217. showTime: true,
  218. opacity: 1,
  219. customShowTimeStyle: {
  220. "background-color": "#000",
  221. color: "#fff",
  222. padding: "5px",
  223. "font-size": "10px",
  224. },
  225. }),
  226. ],
  227. });
  228. // this.wavesurfer.on("seek", (e) => {
  229. // console.log(e, "aaa");
  230. // // console.log(e * 100 + "%", "eee");
  231. // });
  232. //创建region
  233. this.curRegion = this.wavesurfer.addRegion({});
  234. // 特别提醒:此处需要使用require(相对路径),否则会报错
  235. this.wavesurfer.load(require(""));
  236. // this.wavesurfer.load(this.voiceSrc);
  237. let that = this;
  238. window.onresize = () => {
  239. return (() => {
  240. window.screenWidth = document.body.clientWidth;
  241. that.screenWidth = window.screenWidth;
  242. })();
  243. };
  244. });
  245. },
  246. methods: {
  247. playMusic() {
  248. //"播放/暂停"按钮的单击触发事件,暂停的话单击则播放,正在播放的话单击则暂停播放
  249. this.wavesurfer.playPause.bind(this.wavesurfer)();
  250. },
  251. play() {
  252. // 循环播放该区域
  253. this.curRegion.playLoop();
  254. },
  255. //波形图缩放
  256. zoom(val) {
  257. console.log(val);
  258. this.wavesurfer.zoom(val);
  259. },
  260. // 前进,
  261. forward() {
  262. this.wavesurfer.skip(2);
  263. },
  264. // 设置音量:
  265. setVolume(val) {
  266. this.wavesurfer.setVolume(val / 100);
  267. console.log(val);
  268. // getVolume() –返回当前音频片段的音量。
  269. // console.log(this.wavesurfer.getVolume());
  270. },
  271. //倍速播放
  272. DoubleSpeed(rate) {
  273. this.wavesurfer.setPlaybackRate(rate);
  274. // console.log(rate);
  275. },
  276. },
  277. };
  278. </script>
  279. <!-- Add "scoped" attribute to limit CSS to this component only -->
  280. <style scoped>
  281. .myplay {
  282. position: absolute;
  283. top: 50%;
  284. font-size: 17px;
  285. margin: -10px 0 0 -9px;
  286. left: 50%;
  287. }
  288. .mystop {
  289. position: absolute;
  290. top: 50%;
  291. margin: -15px 0 0 -18px;
  292. left: 50%;
  293. font-size: 25px;
  294. }
  295. #waveform {
  296. width: calc(100%);
  297. height: 128px;
  298. float: left;
  299. margin-right: 22px;
  300. background: #fff;
  301. }
  302. /* 去除padding */
  303. /deep/.box-card .el-card__body {
  304. padding: 10px 0px !important;
  305. }
  306. /* 美化滚动条 */
  307. /deep/#waveform ::-webkit-scrollbar {
  308. height: 10px;
  309. background-color: #ccc;
  310. }
  311. /deep/#waveform ::-webkit-scrollbar-thumb {
  312. background-color: #3366ff;
  313. border-radius: 10px;
  314. background-image: -webkit-linear-gradient(
  315. right,blue,#409EFF
  316. );
  317. }
  318. .row {
  319. margin-top: 15px;
  320. }
  321. /deep/#waveform .wavesurfer-region:before {
  322. content: attr(data-region-label);
  323. position: absolute;
  324. top: 0;
  325. padding: 3px 10px 3px;
  326. }
  327. .play {
  328. position: relative;
  329. width: 128px;
  330. height: 128px;
  331. border-radius: 3px;
  332. background-color: #ebeef5;
  333. float: left;
  334. text-align: center;
  335. }
  336. .play p {
  337. margin-top: 85px;
  338. color: #3683fa;
  339. }
  340. .waveformOuter {
  341. margin-bottom: 20px;
  342. overflow: hidden;
  343. }
  344. </style>

遇到的问题
如何在wavesurfer绘制的region上显示文本

参考如何在wavesurfer绘制的region上显示文本 - 知乎

但是加css样式的时候加不上

解决方法:

播放音频选中区域

this.curRegion.playLoop(); //循环播放该区域

this.curRegion.play(); //从start到播放该区域一次end。

发现一个好用的音频字幕网站点滴字幕-自动字幕生成 字幕格式转换 文本语音对齐
如何使用wavesurfer中Cursor(光标)插件

import CursorPlugin from "wavesurfer.js/dist/plugin/wavesurfer.cursor.js";

  1. // 光标插件
  2. CursorPlugin.create({
  3. showTime: true,
  4. opacity: 0.5,
  5. customShowTimeStyle: {
  6. "background-color": "#000",
  7. color: "#fff",
  8. padding: "5px",
  9. "font-size": "10px",
  10. },
  11. }),

美化wavesurfer横向滚动条
  1. /* 美化滚动条 */
  2. /deep/#waveform ::-webkit-scrollbar {
  3. height: 10px;
  4. background-color: #ccc;
  5. }
  6. /deep/#waveform ::-webkit-scrollbar-thumb {
  7. background-color: #3366ff;
  8. border-radius: 10px;
  9. background-image: -webkit-linear-gradient(right, blue, #409eff);
  10. }

Region插件拖拽之后 时间(开始和结束)更改

this.curRegion.update({ start: this.curStart, end: this.curEnd });

Region选中区域改变触发

  1. //方法
  2. saveRegions() {
  3. this.list = this.wavesurfer.regions.list;
  4. // const time = this.wavesurfer.getCurrentTime();
  5. // let min = null;
  6. Object.keys(this.list).forEach((id) => {
  7. const cur = this.list[id];
  8. this.list = cur;
  9. });
  10. this.curStart = this.list.start.toFixed(3);
  11. this.curEnd = this.list.end.toFixed(3);
  12. },
  1. //在需要获取region区域的地方调用
  2. this.wavesurfer.on("region-updated", this.saveRegions);

...待增加

wavesurfer官网文档:wavesurfer.js

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