divclass="mixin-components-container">el-row>el-cardclass="box-card"style="text-align:left">divid="waveform"ref="waveform">div>..._vue wavesurfer分片渲染">
当前位置:   article > 正文

在vue中使用音频wavesurfer.js较全解析_vue wavesurfer分片渲染

vue wavesurfer分片渲染

  1. npm install  wavesurfer.js
  2. template>
  3.   div class="container">
  4.     div class="mixin-components-container">
  5.       el-row>
  6.         el-card class="box-card" style="text-align:left">
  7.           div id="waveform" ref="waveform">
  8.             
  9.           div>
  10.           div id="wave-timeline" ref="wave-timeline">
  11.             
  12.           div>
  13.           div>
  14.             el-button type="primary" @click="rew">后退el-button>
  15.             el-button type="primary" @click="plays">
  16.               i class="el-icon-video-play">i>
  17.               播放
  18.             el-button>
  19.             el-button type="primary" @click="pause">
  20.               i class="el-icon-video-pause">i> 暂停el-button
  21.             >
  22.             el-button type="primary" @click="speek">前进el-button>
  23.             el-button type="primary" @click="replay">重放el-button>
  24.             el-tooltip
  25.               class="item"
  26.               effect="dark"
  27.               content="指定播放"
  28.               placement="bottom"
  29.             >
  30.               el-popover placement="top" width="200" trigger="click">
  31.                 el-input
  32.                   v-model="appointTime"
  33.                   placeholder="请输入内容"
  34.                   class="input-with-select"
  35.                 >
  36.                   el-button slot="append" @click="appointPlay">播放el-button>
  37.                 el-input>
  38.                 el-button slot="reference" circle>
  39.                   指定播放
  40.                 el-button>
  41.               el-popover>
  42.             el-tooltip>
  43.             span
  44.               style="border: 2px solid #2f4f4f;margin-left: 8px;margin-right: 4px"
  45.             />
  46.             el-tooltip
  47.               class="item"
  48.               effect="dark"
  49.               content="音量"
  50.               placement="bottom"
  51.             >
  52.               el-popover
  53.                 placement="top-start"
  54.                 trigger="click"
  55.                 style="min-width: 38px;margin-left: 10px"
  56.               >
  57.                 div class="block" style="width: 42px">
  58.                   el-slider
  59.                     v-model="value"
  60.                     vertical
  61.                     height="100px"
  62.                     @change="setVolume"
  63.                   />
  64.                 div>
  65.                 el-button slot="reference" circle>
  66.                   音量
  67.                 el-button>
  68.               el-popover>
  69.             el-tooltip>
  70.             el-tooltip
  71.               class="item"
  72.               effect="dark"
  73.               content="播放倍速"
  74.               placement="bottom"
  75.             >
  76.               el-popover
  77.                 placement="top"
  78.                 width="220"
  79.                 trigger="click"
  80.                 style="margin-left: 10px"
  81.               >
  82.                 el-input-number
  83.                   v-model="ds"
  84.                   width="180"
  85.                   :precision="2"
  86.                   :step="0.1"
  87.                   :min="0.5"
  88.                   :max="2"
  89.                   @change="DoubleSpeed"
  90.                 />
  91.                 el-button slot="reference" round>
  92.                   {{ ds + " X" }}
  93.                 el-button>
  94.               el-popover>
  95.             el-tooltip>
  96.           div>
  97.         el-card>
  98.       el-row>
  99.     div>
  100.   div>
  101. template>
  102. script>
  103. import WaveSurfer from "wavesurfer.js"//导入wavesurfer.js
  104. import Timeline from "wavesurfer.js/dist/plugin/wavesurfer.timeline.js"//导入时间轴插件
  105. import CursorPlugin from "wavesurfer.js/dist/plugin/wavesurfer.cursor.js";
  106. import RegionsPlugin from "wavesurfer.js/dist/plugin/wavesurfer.regions.min.js";
  107. export default {
  108.   name,
  109.   components: {},
  110.   data() {
  111.     // 这里存放数据
  112.     return {
  113.       wavesurfernull,
  114.       // 指定播放功能的播放时间点
  115.       appointTime1,
  116.       // 播放倍速
  117.       ds1.0,
  118.       // 设置音量
  119.       value0,
  120.     };
  121.   },
  122.   created() {},
  123.   mounted() {
  124.     this.$nextTick(() => {
  125.       console.log(WaveSurfer);
  126.       this.wavesurfer = WaveSurfer.create({
  127.         // 应该在其中绘制波形的CSS选择器或HTML元素。这是唯一必需的参数。
  128.         containerthis.$refs.waveform,
  129.         // 光标的填充颜色,指示播放头的位置。
  130.         cursorColor"red",
  131.         // 更改波形容器的背景颜色。
  132.         backgroundColor"gray",
  133.         // 光标后的波形填充颜色。
  134.         waveColor"violet",
  135.         // 光标后面的波形部分的填充色。当progressColor和waveColor相同时,完全不渲染进度波
  136.         progressColor"purple",
  137.         backend"MediaElement",
  138.         // 音频播放时间轴
  139.         mediaControlsfalse,
  140.         // 播放音频的速度
  141.         audioRate"1",
  142.         // 插件:此教程配置了光标插件和时间轴插件
  143.         plugins: [
  144.           // 光标插件
  145.           CursorPlugin.create({
  146.             showTimetrue,
  147.             opacity1,
  148.             customShowTimeStyle: {
  149.               "background-color""#000",
  150.               color"#fff",
  151.               padding"2px",
  152.               "font-size""10px",
  153.             },
  154.           }),
  155.           RegionsPlugin.create(),
  156.           // 时间轴插件
  157.           Timeline.create({
  158.             container"#wave-timeline",
  159.           }),
  160.         ],
  161.       });
  162.       this.wavesurfer.on("error"function(e) {
  163.         console.warn(e);
  164.       });
  165.       this.wavesurfer.on("interaction "function(e) {
  166.         console.warn(e);
  167.       });
  168.      // this.wavesurfer.addRegion({
  169.         id: id,
  170.         start: startime,
  171.         end"endtime",
  172.         loopfalse,
  173.         dragfalse,
  174.         resizefalse,
  175.         color"rgba(254, 255, 255, 0.4)",
  176.       });
  177.       //标注某个区域
  178.       this.wavesurfer.addRegion({
  179.         id1,
  180.         start"10",
  181.         end"20",
  182.         loopfalse,
  183.         dragfalse,
  184.         resizefalse,
  185.         color"rgba(254, 255, 255, 0.4)",
  186.       });
  187.       this.wavesurfer.addRegion({
  188.         id2,
  189.         start"30",
  190.         end"60",
  191.         loopfalse,
  192.         dragfalse,
  193.         resizefalse,
  194.         color"rgba(253, 251, 25, 0.4)",
  195.       });
  196.       //总时常
  197.       let duration = parseInt(this.wavesurfer.getDuration());
  198.       console.log(duration);
  199.       console.log(this.wavesurfer.getCurrentTime());
  200.       this.wavesurfer.load(require("./../../assets/logo.mp3"));
  201.       // this.value = this.wavesurfer.getVolume() * 100
  202.     });
  203.   },
  204.   // 方法集合
  205.   methods: {
  206.     add() {
  207.       this.$store.commit("addcount"1);
  208.     },
  209.     del() {
  210.       this.$store.commit("delcount"1);
  211.     },
  212.     // 播放时暂停,播放时暂停
  213.     plays() {
  214.       this.wavesurfer.play();
  215.     },
  216.     //暂停
  217.     pause() {
  218.       this.wavesurfer.playPause();
  219.     },
  220.     //
  221.     // 后退,
  222.     rew() {
  223.       this.wavesurfer.skip(-3);
  224.     },
  225.     // 前进,
  226.     speek() {
  227.       this.wavesurfer.skip(3);
  228.     },
  229.     // 重放
  230.     replay() {
  231.       this.wavesurfer.stop();
  232.     },
  233.     // 设置音量:
  234.     setVolume(val) {
  235.       this.wavesurfer.setVolume(val / 100);
  236.     },
  237.     // 指定播放
  238.     appointPlay() {
  239.       this.wavesurfer.play([this.appointTime]);
  240.     },
  241.     DoubleSpeed() {
  242.       console.log("点击倍数加减触发");
  243.     },
  244.   },
  245.   computed: {},
  246.   watch: {},
  247. };
  248. script>
  249. style scoped>
  250. /* @import url(); 引入公共css类 */
  251. .mixin-components-container {
  252.   background-color: #f0f2f5;
  253.   padding: 30px;
  254.   min-heightcalc(100vh - 84px);
  255. }
  256. style>

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

闽ICP备14008679号