gaoluyang
2 天以前 787ccc59ba89bacc075562a161ecf02bc76ebadc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<script lang="ts" setup>
import type { MusicSong } from '../types';
 
import { computed, inject, nextTick, reactive, ref, watch } from 'vue';
 
import { IconifyIcon } from '@vben/icons';
 
import { Image, Slider } from 'ant-design-vue';
 
import { currentSongKey } from '../types';
 
defineOptions({ name: 'AiMusicAudioBarIndex' });
 
const currentSong = inject(currentSongKey, ref<MusicSong>({}));
const currentAudioUrl = computed(() => currentSong.value.audioUrl || undefined);
 
const audioRef = ref<HTMLAudioElement | null>(null);
const audioProgress = ref(0);
const audioDuration = ref(0);
const audioProps = reactive<any>({
  autoplay: true,
  paused: false,
  currentTime: '00:00',
  duration: '00:00',
  muted: false,
  volume: 50,
}); // 音频相关属性https://www.runoob.com/tags/ref-av-dom.html
 
function formatAudioTime(seconds: number) {
  if (!Number.isFinite(seconds)) {
    return '00:00';
  }
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = Math.floor(seconds % 60);
  return `${minutes.toString().padStart(2, '0')}:${remainingSeconds
    .toString()
    .padStart(2, '0')}`;
}
 
function toggleStatus(type: string) {
  audioProps[type] = !audioProps[type];
  if (type === 'paused' && audioRef.value) {
    if (audioProps[type]) {
      audioRef.value.pause();
    } else {
      audioRef.value.play();
    }
  }
}
 
/** 更新播放位置 */
function audioTimeUpdate() {
  if (!audioRef.value) {
    return;
  }
  audioProgress.value = audioRef.value.currentTime;
  audioProps.currentTime = formatAudioTime(audioRef.value.currentTime);
}
 
function audioLoadedMetadata() {
  if (!audioRef.value) {
    return;
  }
  audioDuration.value = audioRef.value.duration;
  audioProps.duration = formatAudioTime(audioRef.value.duration);
}
 
function handleProgressChange(value: number | [number, number]) {
  if (!audioRef.value || Array.isArray(value)) {
    return;
  }
  audioRef.value.currentTime = value;
  audioProgress.value = value;
  audioProps.currentTime = formatAudioTime(value);
}
 
watch(currentAudioUrl, () => {
  audioProgress.value = 0;
  audioDuration.value = 0;
  audioProps.currentTime = '00:00';
  audioProps.duration = '00:00';
  nextTick(() => {
    audioRef.value?.load();
  });
});
</script>
 
<template>
  <div
    class="b-1 b-l-none h-18 flex items-center justify-between border border-solid border-rose-100 bg-card px-2"
  >
    <!-- 歌曲信息 -->
    <div class="flex gap-2.5">
      <Image
        src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
        :width="45"
      />
      <div>
        <div>{{ currentSong.title || '暂无音乐' }}</div>
        <div class="text-xs text-gray-400">
          {{ currentSong.singer || currentSong.desc }}
        </div>
      </div>
    </div>
    <!-- 音频controls -->
    <div class="flex items-center gap-3">
      <IconifyIcon
        icon="majesticons:back-circle"
        class="size-5 cursor-pointer text-gray-300"
      />
      <IconifyIcon
        :icon="
          audioProps.paused
            ? 'mdi:arrow-right-drop-circle'
            : 'solar:pause-circle-bold'
        "
        class="size-7 cursor-pointer"
        @click="toggleStatus('paused')"
      />
      <IconifyIcon
        icon="majesticons:next-circle"
        class="size-5 cursor-pointer text-gray-300"
      />
      <div class="flex items-center gap-4">
        <span>{{ audioProps.currentTime }}</span>
        <Slider
          v-model:value="audioProgress"
          :max="audioDuration"
          color="#409eff"
          class="!w-40"
          @change="handleProgressChange"
        />
        <span>{{ audioProps.duration }}</span>
      </div>
      <!-- 音频 -->
      <audio
        :src="currentAudioUrl"
        :autoplay="audioProps.autoplay"
        :muted="audioProps.muted"
        ref="audioRef"
        controls
        v-show="!audioProps"
        @timeupdate="audioTimeUpdate"
        @loadedmetadata="audioLoadedMetadata"
      ></audio>
    </div>
    <div class="flex items-center gap-4">
      <IconifyIcon
        :icon="audioProps.muted ? 'tabler:volume-off' : 'tabler:volume'"
        class="size-5 cursor-pointer"
        @click="toggleStatus('muted')"
      />
      <Slider v-model:value="audioProps.volume" color="#409eff" class="!w-40" />
    </div>
  </div>
</template>