当前位置: 代码网 > it编程>前端脚本>Vue.js > VUE3开箱即用的音频播放组件完整代码(基于原生audio)

VUE3开箱即用的音频播放组件完整代码(基于原生audio)

2024年10月28日 Vue.js 我要评论
1 效果展示2 组件文件audiobase.vue注:我是使用unocss语法来编写css 如果项目没有搭载unocss 将其相应的转换为css即可<template> <div

1 效果展示

2 组件文件

audiobase.vue

注:我是使用unocss语法来编写css 如果项目没有搭载unocss 将其相应的转换为css即可

<template>
  <div
    :style="{
      height: `${100 + (textdiv ? textdiv.clientheight + 10 : 0)}px`,
    }"
    p-10
    bg-white
    w-500
    rounded-xl
    border="1px solid gray-200 "
  >
    <div>
      <div inline-flex text-size-6xl>
        与
        <div mx-5 inline-flex>{{ $props.customername }}</div>
        进行通话
      </div>
      <div text-gray-500 inline-flex float-right>{{ $props.calltime }}</div>
    </div>
    <div class="bg-[--td-brand-color-1]" float-left flex-center pl-9 pr-11 rounded-55 w-330 h-40 mt-10>
      <span w-50 inline-block mr-8> {{ transtime(audiocurrent) }} </span>
      <div class="progress" w-120 mx-10 my-0>
        <t-slider
          v-model="playprogress"
          :tooltip-props="{
            theme: 'light',
            content: transtime((changeval * audioduration) / 100),
          }"
          @change="change"
          @change-end="changeend"
        />
      </div>
      <span w-50 inline-block mr-6>{{ transtime(audioduration) }}</span>
      <t-icon
        v-if="!playstatus"
        name="play-circle"
        class="color-[--td-brand-color-6]"
        w-34
        h-34
        ml-7
        cursor-pointer
        @click="onplay"
      ></t-icon>
      <t-icon
        v-else
        class="color-[--td-brand-color-6]"
        w-34
        h-34
        ml-7
        cursor-pointer
        name="pause-circle"
        @click="onpause"
      ></t-icon>
    </div>
    <audio ref="audioref" :src="url" @canplay="oncanplay" />
    <div ml-20 flex-center mt-10>
      <t-popup v-model:visible="speedvisible" placement="top" :width="50">
        <template #content>
          <div
            v-for="item in speedlist"
            :key="item.value"
            mb-17
            cursor-pointer
            text-center
            @click="onchangespeed(item.value)"
          >
            <span>{{ item.label }}</span>
          </div>
        </template>
        <div inline ml-5 cursor-pointer>
          <span class="text-[var(--td-brand-color-6)]" cursor-pointer @click="onhandlespeed"
            >{{ activespeed.tofixed(1) }}x</span
          >
          <div color-gray-6 text-12>倍速</div>
        </div>
      </t-popup>
      <div inline ml-20 flex-col-center cursor-pointer>
        <t-icon class="color-[var(--td-brand-color-6)]" ml-4 w-20 h-18 name="cloud-download"></t-icon>
        <div color-gray-6 text-12>下载</div>
      </div>
      <div inline ml-20 flex-col-center cursor-pointer @click="handletext">
        <t-icon class="color-[var(--td-brand-color-6)]" ml-5 w-20 h-18 name="translate"></t-icon>
        <div color-gray-6 text-12>转文字</div>
      </div>
    </div>
    <div
      v-if="showtext"
      ref="textdiv"
      class="bg-[var(--td-brand-color-1)]"
      p-10
      rounded-5
      mt-10
      max-h-190
      overflow-auto
      overflow-y-auto
    >
      {{ $props.text }}
    </div>
  </div>
</template>

<script lang="ts" setup>
import moment from 'moment';
import { defineprops, onmounted, ref } from 'vue';

defineprops({
  customername: {
    type: string,
    default: 'xxx',// 名字
  },
  calltime: {
    type: string,
    default: '2024-08-02 16:20:35',
  },
  url: {
    type: string,
    default: 'http://music.163.com/song/media/outer/url?id=1337065812.mp3',
  },
  text: {
    type: string,
    default: '默认的内容还可以吧我是默认的语音转文字内容看看长度如何呢默认的内容还可以吧我是默认的语音转文字',
  },
});

const speedlist = [
  {
    label: '2x',
    value: 2,
  },
  {
    label: '1.5x',
    value: 1.5,
  },
  {
    label: '1x',
    value: 1,
  },
  {
    label: '0.75x',
    value: 0.75,
  },
];

onmounted(() => {
  clearinterval(timeinterval.value);
});

const speedvisible = ref<boolean>(false); // 设置音频播放速度弹窗
const audioref = ref(); // 音频标签对象
const activespeed = ref(1); // 音频播放速度
const audioduration = ref(0); // 音频总时长
const audiocurrent = ref(0); // 音频当前播放时间
const playstatus = ref<boolean>(false); // 音频播放状态:true 播放,false 暂停
const playprogress = ref(0); // 音频播放进度
const timeinterval = ref(); // 获取音频播放进度定时器
const showtext = ref(false);
const textdiv = ref();
const changeval = ref(0);
/** function */
// 拖动进度条
const change = (val) => {
  changeval.value = val;
};
const changeend = (val: number) => {
  if (audioduration.value === 0) return;
  playprogress.value = val;
  audioref.value.currenttime = (playprogress.value / 100) * audioduration.value;
  audiocurrent.value = audioref.value.currenttime;
};
// 文字展示
const handletext = async () => {
  showtext.value = !showtext.value;
};
// 音频加载完毕的回调
const oncanplay = () => {
  audioduration.value = audioref?.value.duration || 0;
};
const onplay = async () => {
  // 音频播放完后,重新播放
  if (playprogress.value === 100) audioref.value.currenttime = 0;

  await audioref.value.play();
  playstatus.value = true;
  audioduration.value = audioref.value.duration;

  timeinterval.value = setinterval(() => {
    audiocurrent.value = audioref.value.currenttime;
    playprogress.value = (audiocurrent.value / audioduration.value) * 100;
    if (playprogress.value === 100) onpause();
  }, 100);
};
const onpause = () => {
  audioref.value.pause();
  playstatus.value = false;
  clearinterval(timeinterval.value);
};
const onchangespeed = (value: number) => {
  activespeed.value = value;
  // 设置倍速
  audioref.value.playbackrate = value;
  speedvisible.value = false;
};
const onhandlespeed = () => {
  speedvisible.value = !speedvisible.value;
};
// 音频播放时间换算
const transtime = (value: number) => {
  return `${math.floor(value / 3600) > 9 ? math.floor(value / 3600) : `0${math.floor(value / 3600)}`}:${moment({
    m: moment.duration(value, 'seconds').minutes(),
    s: moment.duration(value, 'seconds').seconds(),
  }).format('mm:ss')}`;
};
</script>

<style lang="less" scoped>
.progress {
  ::v-deep .sp-slider__button-wrapper {
    width: 20px;
    height: 20px;
    margin-top: 8px;
  }
  ::v-deep .sp-slider__button {
    width: 12px;
    height: 12px;
    background-color: #4974f5;
  }
}
</style>

3 使用尝试

在外层父组件中使用如下:

先引入组件

<script setup lang="ts">
import audiobase from '@/layouts/components/basecomponents/audiobase.vue';
</script>

然后直接使用

<div bg-white p-10 rounded-5>
   播放组件:
   <audio-base></audio-base>
</div>

总结 

到此这篇关于vue3音频播放组件的文章就介绍到这了,更多相关vue3音频播放组件(原生audio)内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com