0


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

1 效果展示

2 组件文件

AudioBase.vue

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

  1. <template>
  2. <div
  3. :style="{
  4. height: `${100 + (textDiv ? textDiv.clientHeight + 10 : 0)}px`,
  5. }"
  6. p-10
  7. bg-white
  8. w-500
  9. rounded-xl
  10. border="1px solid gray-200 "
  11. >
  12. <div>
  13. <div inline-flex text-size-6xl>
  14. <div mx-5 inline-flex>{{ $props.customerName }}</div>
  15. 进行通话
  16. </div>
  17. <div text-gray-500 inline-flex float-right>{{ $props.callTime }}</div>
  18. </div>
  19. <div class="bg-[--td-brand-color-1]" float-left flex-center pl-9 pr-11 rounded-55 w-330 h-40 mt-10>
  20. <span w-50 inline-block mr-8> {{ transTime(audioCurrent) }} </span>
  21. <div class="progress" w-120 mx-10 my-0>
  22. <t-slider
  23. v-model="playProgress"
  24. :tooltip-props="{
  25. theme: 'light',
  26. content: transTime((changeVal * audioDuration) / 100),
  27. }"
  28. @change="change"
  29. @change-end="changeEnd"
  30. />
  31. </div>
  32. <span w-50 inline-block mr-6>{{ transTime(audioDuration) }}</span>
  33. <t-icon
  34. v-if="!playStatus"
  35. name="play-circle"
  36. class="color-[--td-brand-color-6]"
  37. w-34
  38. h-34
  39. ml-7
  40. cursor-pointer
  41. @click="onPlay"
  42. ></t-icon>
  43. <t-icon
  44. v-else
  45. class="color-[--td-brand-color-6]"
  46. w-34
  47. h-34
  48. ml-7
  49. cursor-pointer
  50. name="pause-circle"
  51. @click="onPause"
  52. ></t-icon>
  53. </div>
  54. <audio ref="audioRef" :src="url" @canplay="onCanplay" />
  55. <div ml-20 flex-center mt-10>
  56. <t-popup v-model:visible="speedVisible" placement="top" :width="50">
  57. <template #content>
  58. <div
  59. v-for="item in speedList"
  60. :key="item.value"
  61. mb-17
  62. cursor-pointer
  63. text-center
  64. @click="onChangeSpeed(item.value)"
  65. >
  66. <span>{{ item.label }}</span>
  67. </div>
  68. </template>
  69. <div inline ml-5 cursor-pointer>
  70. <span class="text-[var(--td-brand-color-6)]" cursor-pointer @click="onHandleSpeed"
  71. >{{ activeSpeed.toFixed(1) }}x</span
  72. >
  73. <div color-gray-6 text-12>倍速</div>
  74. </div>
  75. </t-popup>
  76. <div inline ml-20 flex-col-center cursor-pointer>
  77. <t-icon class="color-[var(--td-brand-color-6)]" ml-4 w-20 h-18 name="cloud-download"></t-icon>
  78. <div color-gray-6 text-12>下载</div>
  79. </div>
  80. <div inline ml-20 flex-col-center cursor-pointer @click="handleText">
  81. <t-icon class="color-[var(--td-brand-color-6)]" ml-5 w-20 h-18 name="translate"></t-icon>
  82. <div color-gray-6 text-12>转文字</div>
  83. </div>
  84. </div>
  85. <div
  86. v-if="showText"
  87. ref="textDiv"
  88. class="bg-[var(--td-brand-color-1)]"
  89. p-10
  90. rounded-5
  91. mt-10
  92. max-h-190
  93. overflow-auto
  94. overflow-y-auto
  95. >
  96. {{ $props.text }}
  97. </div>
  98. </div>
  99. </template>
  100. <script lang="ts" setup>
  101. import moment from 'moment';
  102. import { defineProps, onMounted, ref } from 'vue';
  103. defineProps({
  104. customerName: {
  105. type: String,
  106. default: 'xxx',// 名字
  107. },
  108. callTime: {
  109. type: String,
  110. default: '2024-08-02 16:20:35',
  111. },
  112. url: {
  113. type: String,
  114. default: 'http://music.163.com/song/media/outer/url?id=1337065812.mp3',
  115. },
  116. text: {
  117. type: String,
  118. default: '默认的内容还可以吧我是默认的语音转文字内容看看长度如何呢默认的内容还可以吧我是默认的语音转文字',
  119. },
  120. });
  121. const speedList = [
  122. {
  123. label: '2x',
  124. value: 2,
  125. },
  126. {
  127. label: '1.5x',
  128. value: 1.5,
  129. },
  130. {
  131. label: '1x',
  132. value: 1,
  133. },
  134. {
  135. label: '0.75x',
  136. value: 0.75,
  137. },
  138. ];
  139. onMounted(() => {
  140. clearInterval(timeInterval.value);
  141. });
  142. const speedVisible = ref<boolean>(false); // 设置音频播放速度弹窗
  143. const audioRef = ref(); // 音频标签对象
  144. const activeSpeed = ref(1); // 音频播放速度
  145. const audioDuration = ref(0); // 音频总时长
  146. const audioCurrent = ref(0); // 音频当前播放时间
  147. const playStatus = ref<boolean>(false); // 音频播放状态:true 播放,false 暂停
  148. const playProgress = ref(0); // 音频播放进度
  149. const timeInterval = ref(); // 获取音频播放进度定时器
  150. const showText = ref(false);
  151. const textDiv = ref();
  152. const changeVal = ref(0);
  153. /** FUNCTION */
  154. // 拖动进度条
  155. const change = (val) => {
  156. changeVal.value = val;
  157. };
  158. const changeEnd = (val: number) => {
  159. if (audioDuration.value === 0) return;
  160. playProgress.value = val;
  161. audioRef.value.currentTime = (playProgress.value / 100) * audioDuration.value;
  162. audioCurrent.value = audioRef.value.currentTime;
  163. };
  164. // 文字展示
  165. const handleText = async () => {
  166. showText.value = !showText.value;
  167. };
  168. // 音频加载完毕的回调
  169. const onCanplay = () => {
  170. audioDuration.value = audioRef?.value.duration || 0;
  171. };
  172. const onPlay = async () => {
  173. // 音频播放完后,重新播放
  174. if (playProgress.value === 100) audioRef.value.currentTime = 0;
  175. await audioRef.value.play();
  176. playStatus.value = true;
  177. audioDuration.value = audioRef.value.duration;
  178. timeInterval.value = setInterval(() => {
  179. audioCurrent.value = audioRef.value.currentTime;
  180. playProgress.value = (audioCurrent.value / audioDuration.value) * 100;
  181. if (playProgress.value === 100) onPause();
  182. }, 100);
  183. };
  184. const onPause = () => {
  185. audioRef.value.pause();
  186. playStatus.value = false;
  187. clearInterval(timeInterval.value);
  188. };
  189. const onChangeSpeed = (value: number) => {
  190. activeSpeed.value = value;
  191. // 设置倍速
  192. audioRef.value.playbackRate = value;
  193. speedVisible.value = false;
  194. };
  195. const onHandleSpeed = () => {
  196. speedVisible.value = !speedVisible.value;
  197. };
  198. // 音频播放时间换算
  199. const transTime = (value: number) => {
  200. return `${Math.floor(value / 3600) > 9 ? Math.floor(value / 3600) : `0${Math.floor(value / 3600)}`}:${moment({
  201. m: moment.duration(value, 'seconds').minutes(),
  202. s: moment.duration(value, 'seconds').seconds(),
  203. }).format('mm:ss')}`;
  204. };
  205. </script>
  206. <style lang="less" scoped>
  207. .progress {
  208. ::v-deep .sp-slider__button-wrapper {
  209. width: 20px;
  210. height: 20px;
  211. margin-top: 8px;
  212. }
  213. ::v-deep .sp-slider__button {
  214. width: 12px;
  215. height: 12px;
  216. background-color: #4974f5;
  217. }
  218. }
  219. </style>

3 使用尝试

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

先引入组件

  1. <script setup lang="ts">
  2. import AudioBase from '@/layouts/components/baseComponents/AudioBase.vue';
  3. </script>

然后直接使用

  1. <div bg-white p-10 rounded-5>
  2. 播放组件:
  3. <audio-base></audio-base>
  4. </div>
标签: 音视频 前端 css

本文转载自: https://blog.csdn.net/cute_ming/article/details/141052791
版权归原作者 cute_ming 所有, 如有侵权,请联系我们删除。

“VUE3开箱即用的音频播放组件(基于原生audio)”的评论:

还没有评论