AudioPlayer.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <div class="audio-player">
  3. <!-- 播放器顶部显示标题 -->
  4. <!-- <div class="audio-title">音频播放: 12月24日录音</div> -->
  5. <!-- 音频控制区 -->
  6. <div class="audio-progress">
  7. <div class="time">{{ formatTime(currentTime) }}</div>
  8. <!-- 音频进度条 -->
  9. <div
  10. class="progress-bar"
  11. @mousedown="startDragging"
  12. @mousemove="dragProgress"
  13. @mouseup="endDragging"
  14. @mouseleave="endDragging"
  15. ref="progressBar"
  16. >
  17. <!-- 拖动节点 -->
  18. <div class="progress-node" :style="{ left: `${progress}%` }"></div>
  19. <div class="progress" :style="{ width: `${progress}%` }"></div>
  20. </div>
  21. <!-- 音频总时长 -->
  22. <div class="time">{{ formatTime(duration) }}</div>
  23. </div>
  24. <!-- 播放/暂停按钮 -->
  25. <div class="audio-controls">
  26. <!-- 倍速按钮 -->
  27. <div class="speed-button" @click="toggleSpeed">{{ speedBtn }}&nbsp;x</div>
  28. <!-- 快进/快退按钮 -->
  29. <div class="play-controls">
  30. <img src="../../../assets/image/home/fast_back.png" class="seek-button" @click="seek(-10)" />
  31. <div class="play-button" @click="togglePlay">
  32. <icon-pause-circle class="icon-play" v-if="isPlaying" />
  33. <icon-play-circle-fill class="icon-play" v-else />
  34. </div>
  35. <img src="../../../assets/image/home/fast_back.png" class="seek-button" @click="seek(10)" />
  36. </div>
  37. <div class="setting-button">
  38. <img class="setting-icon" @click="handleHotClick" src="../../../assets/image/home/hot.png" />
  39. <!-- <img class="setting-icon" @click="handleSettingClick" src="../../assets/image/home/settings.png" /> -->
  40. <AudioSetting></AudioSetting>
  41. </div>
  42. </div>
  43. </div>
  44. <ModelHot v-if="show_hot" :visible.async="show_hot" @close="cancelModelHot"></ModelHot>
  45. </template>
  46. <script setup>
  47. import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
  48. import { formatTime } from '../../../utils/format';
  49. import ModelHot from './ModelHot.vue';
  50. import AudioSetting from './AudioSetting.vue';
  51. const props = defineProps({
  52. audio_player_url: {
  53. type: String,
  54. default: ''
  55. }
  56. });
  57. // 音频相关状态
  58. const audio = ref(null); // 音频对象
  59. const isPlaying = ref(false); // 是否正在播放
  60. const currentTime = ref(0); // 当前播放时间
  61. const duration = ref(0); // 音频总时长
  62. const isDragging = ref(false); // 是否正在拖动进度条
  63. const progressBar = ref(null); // 进度条 DOM 引用
  64. const speed = ref(1); // 播放速度(1x,1.5x,2x等)
  65. // 热词优化
  66. const show_hot = ref(false);
  67. // 进度条的百分比宽度
  68. const progress = computed(() => {
  69. return duration.value ? (currentTime.value / duration.value) * 100 : 0;
  70. });
  71. const speedBtn = computed(() => {
  72. return speed.value.toFixed(1);
  73. });
  74. // 初始化音频
  75. onMounted(() => {
  76. // audio.value = new Audio('https://www.sample-videos.com/audio/mp3/crowd-cheering.mp3');
  77. // console.log('props.audio_player_url', props.audio_player_url);
  78. audio.value = new Audio();
  79. audio.value.src = props.audio_player_url; // 设置音频源
  80. audio.value.load();
  81. // console.log('audio.value', audio.value);
  82. audio.value.addEventListener('timeupdate', updateTime);
  83. audio.value.addEventListener('loadedmetadata', updateDuration);
  84. audio.value.addEventListener('ended', onAudioEnded); // 添加监听播放结束事件
  85. audio.value.playbackRate = speed.value; // 设置初始播放速度
  86. audio.value.addEventListener('loadeddata', () => {
  87. console.log('Audio file loaded successfully');
  88. });
  89. audio.value.addEventListener('error', (e) => {
  90. console.error('Error loading audio:', e);
  91. });
  92. });
  93. // 播放/暂停切换
  94. const togglePlay = () => {
  95. if (isPlaying.value) {
  96. audio.value.pause();
  97. } else {
  98. audio.value.play();
  99. }
  100. isPlaying.value = !isPlaying.value;
  101. };
  102. // 更新当前播放时间
  103. const updateTime = () => {
  104. if (!isDragging.value) {
  105. currentTime.value = audio.value.currentTime;
  106. }
  107. };
  108. // 更新音频总时长
  109. const updateDuration = () => {
  110. duration.value = audio.value.duration;
  111. };
  112. // 开始拖动进度条
  113. const startDragging = (event) => {
  114. isDragging.value = true;
  115. setProgress(event);
  116. };
  117. // 拖动过程中更新进度条
  118. const dragProgress = (event) => {
  119. if (isDragging.value) {
  120. setProgress(event);
  121. // 实时更新音频播放时间
  122. audio.value.currentTime = currentTime.value;
  123. }
  124. };
  125. // 结束拖动进度条
  126. const endDragging = (event) => {
  127. if (isDragging.value) {
  128. setProgress(event);
  129. // 在拖动结束时,更新音频的播放时间
  130. audio.value.currentTime = currentTime.value;
  131. isDragging.value = false;
  132. }
  133. };
  134. // 根据鼠标位置更新进度
  135. const setProgress = (event) => {
  136. const rect = progressBar.value.getBoundingClientRect();
  137. const offsetX = event.clientX - rect.left;
  138. const width = rect.width;
  139. const percentage = Math.min(Math.max(offsetX / width, 0), 1);
  140. currentTime.value = percentage * duration.value;
  141. };
  142. // 切换播放速度
  143. const toggleSpeed = () => {
  144. if (speed.value === 1) {
  145. speed.value = 1.5;
  146. } else if (speed.value === 1.5) {
  147. speed.value = 2;
  148. } else {
  149. speed.value = 1;
  150. }
  151. audio.value.playbackRate = speed.value; // 更新播放速度
  152. };
  153. // 快进/快退功能
  154. const seek = (time) => {
  155. const newTime = currentTime.value + time;
  156. // 确保时间不会小于0且不超过总时长
  157. currentTime.value = Math.max(0, Math.min(newTime, duration.value));
  158. audio.value.currentTime = currentTime.value;
  159. };
  160. // 播放结束处理
  161. const onAudioEnded = () => {
  162. isPlaying.value = false; // 播放结束时更新按钮状态
  163. currentTime.value = 0; // 重置播放时间
  164. };
  165. // 打开热词配置
  166. const handleHotClick = () => {
  167. show_hot.value = true;
  168. };
  169. // 关闭热词配置
  170. const cancelModelHot = () => {
  171. show_hot.value = false;
  172. };
  173. onBeforeUnmount(() => {
  174. audio.value.pause();
  175. audio.value.removeEventListener('timeupdate', updateTime);
  176. audio.value.removeEventListener('loadedmetadata', updateDuration);
  177. audio.value.removeEventListener('ended', onAudioEnded); // 移除事件监听
  178. });
  179. </script>
  180. <style scoped>
  181. .audio-player {
  182. width: 100%;
  183. border-radius: 10px;
  184. padding: 16px;
  185. background-color: var(--bg-body-color);
  186. margin: 0 auto;
  187. box-sizing: border-box;
  188. box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
  189. }
  190. .audio-title {
  191. font-size: 16px;
  192. font-weight: bold;
  193. margin-bottom: 10px;
  194. }
  195. .audio-progress {
  196. display: flex;
  197. align-items: center;
  198. gap: 20px;
  199. }
  200. .progress-bar {
  201. flex: 1;
  202. height: 8px;
  203. background-color: #e0e0e0;
  204. border-radius: 4px;
  205. position: relative;
  206. cursor: pointer;
  207. }
  208. .progress {
  209. height: 100%;
  210. background-color: #ffb124;
  211. transition: width 0.2s ease;
  212. }
  213. .progress-node {
  214. position: absolute;
  215. top: -4px;
  216. width: 12px;
  217. height: 12px;
  218. background-color: #ffb124;
  219. border: 2px solid #fff;
  220. border-radius: 50%;
  221. cursor: pointer;
  222. transition: left 0.2s ease;
  223. z-index: 10;
  224. }
  225. .time {
  226. font-size: 14px;
  227. color: #666;
  228. }
  229. .audio-controls {
  230. display: flex;
  231. justify-content: space-between;
  232. align-items: center;
  233. }
  234. .play-controls {
  235. display: flex;
  236. align-items: center;
  237. justify-content: center;
  238. }
  239. .play-button {
  240. /* background-color: #007bff; */
  241. border: none;
  242. border-radius: 50%;
  243. width: 40px;
  244. height: 40px;
  245. display: flex;
  246. align-items: center;
  247. justify-content: center;
  248. cursor: pointer;
  249. .icon-play {
  250. font-size: 32px;
  251. color: #007bff;
  252. }
  253. }
  254. .speed-button {
  255. font-size: 18px;
  256. font-weight: 600;
  257. color: #868686;
  258. border: none;
  259. border-radius: 5px;
  260. cursor: pointer;
  261. }
  262. .seek-button {
  263. color: white;
  264. border: none;
  265. border-radius: 5px;
  266. padding: 5px 10px;
  267. cursor: pointer;
  268. font-size: 14px;
  269. }
  270. .setting-button {
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. gap: 10px;
  275. }
  276. .setting-icon {
  277. cursor: pointer;
  278. }
  279. </style>