【wavesurfer.js实战范例】多区域音频标注(含区域实时切换显示)

在这里插入图片描述
通过多选框,实现多区域显示的实时切换,实现原理:

  1. 每次切换时先清除所有区域
  2. 对选中区域进行重绘
<template>
  <div style="padding: 30px">
    <el-row type="flex" justify="center">
      <el-checkbox-group v-model="showList" @change="showListChange">
        <el-checkbox
          v-for="(regionInfo, regionKey) in regionDic"
          :label="regionKey"
          :key="'region' + regionKey"
        >
          <div class="colorTag" :style="{ background: regionInfo.color }"></div>
          {{ regionInfo.label }}</el-checkbox
        >
      </el-checkbox-group>
    </el-row>
    <div class="waveBox" ref="wave_Ref"></div>
  </div>
</template>

<script>
import WaveSurfer from "wavesurfer.js";
// 导入插件--区域
import RegionsPlugin from "wavesurfer.js/dist/plugin/wavesurfer.regions.min.js";
// 导入插件--指针轴
import CursorPlugin from "wavesurfer.js/dist/plugin/wavesurfer.cursor.js";

export default {
  data() {
    return {
      showList: ["1", "2", "3", "4", "5"],
      wavesurfer: null,
      regionDic: {
        1: {
          label: "前奏",
          color: "rgba(255, 255, 0, 0.5)",
          dataMark: "preludeList",
        },
        2: {
          label: "男声",
          color: "rgba(0, 0, 255, 0.5)",
          dataMark: "boyVoiceList",
        },
        3: {
          label: "女声",
          color: "rgba(255, 0, 0, 0.5)",
          dataMark: "girlVoiceList",
        },
        4: {
          label: "男/女合唱",
          color: "rgba(0, 255, 255, 0.5)",
          dataMark: "chorusList",
        },
        5: {
          label: "间奏",
          color: "rgba(255, 0, 255, 0.5)",
          dataMark: "interludeList",
        },
      },

      boyVoiceList: [
        {
          start: 14,
          end: 26,
        },
        {
          start: 59,
          end: 64,
        },
        {
          start: 109,
          end: 134,
        },
        {
          start: 148,
          end: 160,
        },
      ],

      girlVoiceList: [
        {
          start: 27,
          end: 39,
        },
        {
          start: 52,
          end: 58,
        },
      ],

      preludeList: [
        {
          start: 0,
          end: 14,
        },
      ],

      // 男女合唱
      chorusList: [
        {
          start: 40,
          end: 52,
        },
        {
          start: 65,
          end: 71,
        },
        {
          start: 97,
          end: 109,
        },
        {
          start: 135,
          end: 147,
        },
        {
          start: 161,
          end: 173,
        },
      ],
      // 间奏
      interludeList: [
        {
          start: 72,
          end: 96,
        },
      ],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.wavesurfer = WaveSurfer.create({
        container: this.$refs.wave_Ref,
        plugins: [
          // 插件--区域的配置
          RegionsPlugin.create({}),

          // 插件--指针轴的配置
          CursorPlugin.create({
            showTime: true,
            opacity: 1,
            customShowTimeStyle: {
              "background-color": "#000",
              color: "#fff",
              padding: "2px",
              "font-size": "10px",
            },
          }),
        ],
      });
      this.wavesurfer.load(require("./《祝福你》群星_粤语.mp3"));
      // 自动播放
      let that = this;
      this.wavesurfer.on("ready", function () {
        that.wavesurfer.play();
      });
      this.drawRegion();
    });
  },
  methods: {
    showListChange() {
      this.wavesurfer.clearRegions();
      this.drawRegion();
    },
    wavesurferAddRegion(start, end, color) {
      this.wavesurfer.addRegion({
        start: start, // 区域开始时间
        end: end, // 区域结束时间
        color: color, // 区域颜色
        drag: false, // 是否可拖拽
        resize: false, // 是否可改变大小
      });
    },
    drawRegion() {
      this.showList.forEach((regionType) => {
        let regionInfo = this.regionDic[regionType];
        this[regionInfo.dataMark].forEach((item) => {
          this.wavesurferAddRegion(item.start, item.end, regionInfo.color);
        });
      });
    },
  },
};
</script>
<style scoped>
.colorTag {
  display: inline-block;
  height: 10px;
  width: 30px;
}
.waveBox {
  border: 1px solid gray;
  margin-top: 15px;
}
</style>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝阳39

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值