当前位置:   article > 正文

vue3中如何使用wavesurfer.js_vue3 wavesurfer container not found

vue3 wavesurfer container not found

案例展示:

在本案例中vue使用的vue3 的setup语法糖的写法。

一、安装wavesurfer.js

  1. npm install wavesurfer.js
  2. 或者
  3. yarn add wavesurfer.js

二、wavesurfer的使用

  1. <script lang="ts" setup>
  2. import { onMounted, ref, nextTick, h } from 'vue';
  3. import WaveSurfer from 'wavesurfer.js/dist/wavesurfer';
  4. import Regions from 'wavesurfer.js/dist/plugins/regions';
  5. import Timeline from 'wavesurfer.js/dist/plugins/timeline';
  6. import _ from 'lodash';
  7. let wavesurfer = ref();
  8. let waveform = ref();
  9. let wsRegions = ref();
  10. let start_time = ref(0);
  11. let table_data = ref<any>([]);
  12. let ws_regions_end = ref<any>(null);
  13. let current_time = ref<string>('00:00:000');
  14. let duration_time = ref<string>('00:00:000');
  15. function getImageUrl() {
  16. return new URL('/src/assets/voice.mp3', import.meta.url).href;
  17. }
  18. const formatTime = (seconds: number) => {
  19. const minutes = Math.floor(seconds / 60);
  20. const remainingSeconds = Math.floor(seconds % 60);
  21. const milliseconds = Math.floor((seconds - Math.floor(seconds)) * 1000);
  22. const formattedMinutes = String(minutes).padStart(2, '0');
  23. const formattedSeconds = String(remainingSeconds).padStart(2, '0');
  24. const formattedMilliseconds = String(milliseconds).padStart(3, '0');
  25. return `${formattedMinutes}:${formattedSeconds}:${formattedMilliseconds}`;
  26. };
  27. const init = () => {
  28. wavesurfer.value = WaveSurfer.create({
  29. container: waveform.value,
  30. cursorColor: 'red', // 播放进行时线条颜色
  31. cursorWidth: 1, // 播放进行时线条宽度
  32. waveColor: '#4986ff', // 未播放的波纹颜色
  33. progressColor: 'blue', // 已播放的波纹颜色
  34. audioRate: 1, // 倍速
  35. height: 200, // 高度
  36. url: getImageUrl()
  37. });
  38. wsRegions.value = wavesurfer.value.registerPlugin(Regions.create());
  39. wavesurfer.value.registerPlugin(
  40. Timeline.create({
  41. height: 20,
  42. primaryLabelInterval: 2,
  43. // insertPosition: 'beforebegin',
  44. style: {
  45. fontSize: '10px',
  46. color: '#6A3274'
  47. }
  48. })
  49. );
  50. };
  51. /* 播放时暂停,暂停时播放 */
  52. function clickMusic() {
  53. wavesurfer.value.playPause();
  54. }
  55. /* 设置开始时间 */
  56. function setStart(current: number) {
  57. if (table_data.value.length <= 0) {
  58. start_time.value = 0;
  59. } else {
  60. // 重组二维数组
  61. let res = table_data.value.map((item: any) => [item.start, item.end]);
  62. /* 检测当前时间是否在二维数组中间 */
  63. let isIn: any = _.some(res, (subArray) => {
  64. let start = subArray[0];
  65. let end = subArray[1];
  66. return current >= start && current <= end;
  67. });
  68. if (isIn) {
  69. // 在已分割的片段内
  70. start_time.value = -1;
  71. alert('不可切割');
  72. } else {
  73. // 不在分割的片段内,且获取start值
  74. let obj = _.minBy(res, (subArray: any) => {
  75. if (subArray[1] < current) {
  76. return Math.abs(subArray[1] - current);
  77. }
  78. });
  79. if (obj) {
  80. start_time.value = obj[1];
  81. } else {
  82. start_time.value = 0;
  83. }
  84. }
  85. }
  86. return start_time.value;
  87. }
  88. /* 剪切 */
  89. function cutMusic() {
  90. wavesurfer.value.pause(); // 暂停播放
  91. let current = wavesurfer.value.getCurrentTime();
  92. let start = setStart(current);
  93. if (start == -1) {
  94. return false;
  95. }
  96. let _item = {
  97. start: start,
  98. end: current,
  99. // content: contentEle.value,
  100. content: `${formatTime(start)}${formatTime(current)}`,
  101. color: 'hsla(200, 50%, 70%, 0.4)',
  102. drag: false,
  103. resize: false
  104. };
  105. console.log(wsRegions.value, '---------');
  106. wsRegions.value.addRegion(_item);
  107. table_data.value = wsRegions.value.regions;
  108. }
  109. function drawRegion() {
  110. table_data.value.forEach((item: any) => {
  111. wsRegions.value.addRegion(item);
  112. });
  113. }
  114. function delRegions(id: string) {
  115. table_data.value = table_data.value.filter((item: any) => item.id != id);
  116. wsRegions.value.clearRegions();
  117. wsRegions.value.regions = [];
  118. drawRegion();
  119. }
  120. function currentPlay(start: number, end: number) {
  121. ws_regions_end.value = end;
  122. wavesurfer.value.setTime(start);
  123. wavesurfer.value.play();
  124. }
  125. async function makedata(res: any, current: number, flag: 'prev' | 'next') {
  126. let index = res.indexOf(current);
  127. if (index == -1) return;
  128. if (flag == 'prev') {
  129. if (index == 0) {
  130. alert('无上一个切割位置');
  131. return;
  132. }
  133. wavesurfer.value.setTime(res[index - 1]);
  134. wavesurfer.value.isPlaying() ? wavesurfer.value.play() : wavesurfer.value.pause();
  135. } else {
  136. if (index == res.length - 1) {
  137. alert('无下一个切割位置');
  138. return;
  139. }
  140. wavesurfer.value.setTime(res[index + 1]);
  141. wavesurfer.value.isPlaying() ? wavesurfer.value.play() : wavesurfer.value.pause();
  142. }
  143. }
  144. async function changeTime(flag: 'prev' | 'next') {
  145. let current = wavesurfer.value.getCurrentTime();
  146. let res: any = [current];
  147. table_data.value.map((item: any) => {
  148. if (res.indexOf(item.start) == -1) {
  149. res.push(item.start);
  150. }
  151. if (res.indexOf(item.end) == -1) {
  152. res.push(item.end);
  153. }
  154. });
  155. res.sort(function (a: any, b: any) {
  156. return a - b;
  157. });
  158. await makedata(res, current, flag);
  159. }
  160. onMounted(() => {
  161. nextTick(() => {
  162. init();
  163. wavesurfer.value.on('audioprocess', () => {
  164. current_time.value = formatTime(wavesurfer.value.getCurrentTime());
  165. if (ws_regions_end.value !== null && wavesurfer.value.getCurrentTime() >= ws_regions_end.value - 0.1) {
  166. wavesurfer.value.pause(); // 暂停播放
  167. wavesurfer.value.setTime(ws_regions_end.value);
  168. current_time.value = formatTime(ws_regions_end.value);
  169. ws_regions_end.value = null;
  170. }
  171. });
  172. wavesurfer.value.on('ready', function () {
  173. duration_time.value = formatTime(wavesurfer.value.getDuration());
  174. });
  175. });
  176. });
  177. </script>

 未解决问题:

区域 wsRegions的content值。官方文档提示可接收string和HTMLElement两种格式。但在插入HTMLElement格式时只显示最后一次添加的内容。(有没有童鞋知道为什么呢?)

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/130201
推荐阅读
相关标签
  

闽ICP备14008679号