当前位置:   article > 正文

Wavesurfer可选中区域循环播放_wavesurfer-js 循环播放

wavesurfer-js 循环播放

1、引入组件

  1. import Wavesurfer from '@/components/Wavesurfer'
  2. export default {
  3. name: 'App',
  4. components: { Wavesurfer },
  5. data: () => ({
  6. fileUrl: ''
  7. }),
  8. methods: {
  9. playVoice(fileUrl) {
  10. this.fileUrl= fileUrl
  11. this.$refs.playAudio.playAu()
  12. }
  13. }
  14. }

2、 使用

  1. <div class="wave">
  2. <Wavesurfer btnShow :height="22" :filePath="fileUrl" ref="playAudio" />
  3. </div>
  4. <el-button @click="playVoice(fileUrl)" type="primary" plain>调听</el-button>

3、组件

  1. <template>
  2. <div class="waveSurfer">
  3. <div ref="wavesurfer" id="waveform">
  4. <progress id="progress" class="progress progress-striped" value="0" max="100"></progress>
  5. </div>
  6. <div id="wave-timeline" ref="wave-timeline">
  7. <!--时间轴 -->
  8. </div>
  9. <div class="palyButton" v-if="wavesurfer">
  10. <el-button type="primary" @click="playAudio">
  11. <i class="el-icon-video-play"></i>
  12. 播放 /
  13. <i class="el-icon-video-pausee"></i>
  14. 暂停
  15. </el-button>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import WaveSurfer from 'wavesurfer.js'
  21. import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.js'
  22. import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js'
  23. import Regions from 'wavesurfer.js/dist/plugin/wavesurfer.regions.js'
  24. export default {
  25. props: {
  26. filePath: String,
  27. height: { type: Number, default: 100 },
  28. },
  29. data() {
  30. return {
  31. wavesurfer: null,
  32. playing: false,
  33. readyState: false
  34. }
  35. },
  36. mounted() {
  37. // this.renderAudio()
  38. },
  39. beforeDestroy() {
  40. this.destroy()
  41. },
  42. watch: {
  43. filePath() {
  44. this.destroy()
  45. this.renderAudio()
  46. }
  47. },
  48. computed: {
  49. playState() {
  50. return this.playing ? 'el-icon-video-pause' : 'el-icon-video-play'
  51. }
  52. },
  53. methods: {
  54. renderAudio() {
  55. this.wavesurfer = WaveSurfer.create({
  56. container: this.$refs.wavesurfer,
  57. height: this.height,
  58. audioRate: 1,
  59. cursorWidth: 1,
  60. barHeight: 5,
  61. mediaControls: false,
  62. waveColor: '#43d996', // 声波color
  63. progressColor: '#43d996', // 已播放声波color
  64. backgroundColor: '#f0f5f9',
  65. cursorColor: '#006de6',
  66. backend: 'MediaElement',
  67. plugins: [
  68. CursorPlugin.create({
  69. showTime: true,
  70. opacity: 1,
  71. customShowTimeStyle: {
  72. 'background-color': '#000',
  73. color: '#fff',
  74. 'font-size': '10px'
  75. }
  76. }),
  77. // 注册的一组插件定义 时间
  78. Timeline.create({
  79. container: '#wave-timeline',
  80. fontSize: 14,
  81. primaryFontColor: '#9191a5',
  82. secondaryFontColor: '#9191a5',
  83. primaryColor: '#9191a5',
  84. secondaryColor: '#9191a5'
  85. }),
  86. Regions.create({})
  87. ]
  88. })
  89. this.wavesurfer.addRegion({
  90. loop: false,
  91. drag: false,
  92. resize: false,
  93. color: 'rgba(254, 255, 255, 0.4)'
  94. })
  95. // 加载进度条
  96. this.wavesurfer.on('loading', function (percents) {
  97. document.getElementById('progress').value = percents
  98. })
  99. this.wavesurfer.on('ready', () => {
  100. this.wavesurfer.enableDragSelection({
  101. color: 'rgba(0, 180, 0, 0.3)'
  102. })
  103. this.wavesurfer.clearRegions() // 音频加载完成
  104. document.getElementById('progress').style.display = 'none'
  105. document.getElementById('progress').value = 0
  106. this.readyState = true
  107. this.setPlayState()
  108. })
  109. this.wavesurfer.on('finish', () => {
  110. this.stopAudio()
  111. this.setPlayState()
  112. })
  113. this.wavesurfer.on('error', () => {
  114. this.readyState = false
  115. })
  116. document.getElementById('waveform').onclick = function () {
  117. this.wavesurfer.clearRegions()
  118. }
  119. // 更新区域时。回调将接收该Region对象。
  120. this.wavesurfer.on('region-updated', function (region) {
  121. region.playLoop() // 循环播放选中区域
  122. })
  123. this.wavesurfer.on('region-created', () => {
  124. this.wavesurfer.clearRegions()
  125. })
  126. },
  127. getWavLoad(filePath) {
  128. filePath && this.wavesurfer.load(filePath)
  129. },
  130. stopAudio() {
  131. this.readyState && this.wavesurfer.stop()
  132. this.setPlayState()
  133. },
  134. playAu() {
  135. this.$nextTick(() => {
  136. this.getWavLoad(this.filePath)
  137. this.wavesurfer.playPause()
  138. this.setPlayState()
  139. })
  140. },
  141. // 播放/暂停 button
  142. playAudio() {
  143. this.$nextTick(() => {
  144. if (!this.isPlaying && !this.readyState) {
  145. this.getWavLoad(this.filePath)
  146. }
  147. this.wavesurfer.playPause()
  148. this.setPlayState()
  149. })
  150. },
  151. setPlayState() {
  152. this.playing = this.wavesurfer.isPlaying()
  153. },
  154. destroy() {
  155. this.wavesurfer && this.stopAudio()
  156. this.wavesurfer && this.wavesurfer.destroy()
  157. this.wavesurfer = null
  158. },
  159. pause() {
  160. this.wavesurfer && this.playing && this.wavesurfer.pause()
  161. }
  162. }
  163. }
  164. </script>
  165. <style lang="scss">
  166. #waveform {
  167. position: relative;
  168. }
  169. #wave-timeline {
  170. height: 6px;
  171. }
  172. #waveform {
  173. width: 100%;
  174. flex-basis: 128px;
  175. flex-shrink: 0;
  176. position: relative;
  177. }
  178. #progress {
  179. position: absolute;
  180. width: 100%;
  181. height: 4px;
  182. top: 48%;
  183. opacity: 0.7;
  184. z-index: 44;
  185. }
  186. .waveSurfer {
  187. width: 100%;
  188. padding-top: 20px;
  189. }
  190. .waveSurfer >>> .el-slider__runway {
  191. margin: 6px 0;
  192. }
  193. .palyButton {
  194. margin-top: 28px;
  195. }
  196. </style>

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

闽ICP备14008679号