vue ----wavesurfer.js 使用

<template>
  <div>
    <!--时间轴 -->
    <div :id="id"></div>

    <el-button type="primary" @click="playMusic">
      <i class="el-icon-video-play"></i>
      播放 /
      <i class="el-icon-video-pausee"></i>
      暂停
    </el-button>
    <div id="waveform" ref="waveform">
      <!-- Here be the waveform -->
    </div>
  </div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
//import Timeline from "wavesurfer.js/dist/plugin/wavesurfer.timeline.js";
import spectrogram from "wavesurfer.js/dist/plugin/wavesurfer.spectrogram.js";
import cursor from "wavesurfer.js/dist/plugin/wavesurfer.cursor.js";

export default {
  name: "sonic",
  props: ["url", "sty"],
  data() {
    return {
      wavesurfer: null,
      id: null,
    };
  },
  //监听父组件转递过来的音频地址
  watch: {
    url() {
      this.wavesurfer.load(this.url);
      this.$emit("duration", this.wavesurfer.getDuration()); // 调用父组件传递过来的方法,同时把数据传递出去
    },
  },
  methods: {
    playMusic() {
      //"播放/暂停"按钮的单击触发事件,暂停的话单击则播放,正在播放的话单击则暂停播放
      this.wavesurfer.playPause.bind(this.wavesurfer)();
    },
    func() {
      if (this.sty == 1) {
        this.id = "wave-timeline";
      } else {
        this.id = "wave-timelin";
      }
      const colormap = require("colormap");
      const colors = colormap({
        colormap: "hot",
        nshades: 256,
        format: "float",
      });
      this.wavesurfer = WaveSurfer.create({
        container: this.$refs.waveform,
        waveColor: "#409EFF",
        progressColor: "#43d996", // 已播放声波color
        //  backend: "MediaElement",
        //   cursorWidth: 2,
        height: 100, //设置音波大小
        //mediaControls: false,
        // maxCanvasHeight: 100,
        audioRate: "1",
        // barGap: 1,
        barWidth: 0.2,
        barHeight: 50, //设置音波的波纹
        backgroundColor: "gray",
        cursorColor: "red",
        fftSamples: 256, //设置音谱的大小
        //使用时间轴插件
        plugins: [
          spectrogram.create({
            // container: "#wave-timeline",
            container: "#" + this.id,
            labels: true,
            colorMap: colors,
          }),
          cursor.create({
            showTime: true,
            opacity: 1,
            customShowTimeStyle: {
              "background-color": "#000",
              color: "#fff",
              padding: "2px",
              "font-size": "16px",
            },
          }),
        ],
      });
      this.wavesurfer.load(this.url);
      setTimeout(() => {
        this.$emit("duration", this.wavesurfer.getDuration()); // 调用父组件传递过来的方法,同时把数据传递出去
      }, 1000);
    },
  },
  mounted() {
    this.func();
  },
  destroyed() {
    this.wavesurfer.stop();
  },
};
</script>

 

 希望各位大脑多提点一下

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值