mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
fix: queue panel fetches data on open, lyrics auto-sync with playback time
- Player store tracks playStartedAt/pausedElapsed for elapsed time - Queue component fetches from server when opened - Lyrics page syncs every 200ms using elapsed time - Lyrics auto-scroll to active line with smooth animation - Translation lines shown below main lyrics - Fade mask on lyrics scroll area Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
67584d2622
commit
9dce883384
3 files changed
+189
-18
No files matched your search
@@ -33,12 +33,13 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { watch } from 'vue';
|
||||||
import { Icon } from '@iconify/vue';
|
import { Icon } from '@iconify/vue';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { usePlayerStore } from '../stores/player.js';
|
import { usePlayerStore } from '../stores/player.js';
|
||||||
import CoverArt from './CoverArt.vue';
|
import CoverArt from './CoverArt.vue';
|
||||||
|
|
||||||
defineProps<{
|
const props = defineProps<{
|
||||||
open: boolean;
|
open: boolean;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
@@ -48,10 +49,16 @@ defineEmits<{
|
|||||||
|
|
||||||
const store = usePlayerStore();
|
const store = usePlayerStore();
|
||||||
|
|
||||||
|
// Fetch queue when panel opens
|
||||||
|
watch(() => props.open, (isOpen) => {
|
||||||
|
if (isOpen) store.fetchQueue();
|
||||||
|
});
|
||||||
|
|
||||||
async function removeSong(index: number) {
|
async function removeSong(index: number) {
|
||||||
if (!store.activeBotId) return;
|
if (!store.activeBotId) return;
|
||||||
try {
|
try {
|
||||||
await axios.delete(`/api/player/${store.activeBotId}/queue/${index}`);
|
await axios.delete(`/api/player/${store.activeBotId}/queue/${index + 1}`);
|
||||||
|
await store.fetchQueue();
|
||||||
} catch {
|
} catch {
|
||||||
// Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
activeBotId: null as string | null,
|
activeBotId: null as string | null,
|
||||||
queue: [] as Song[],
|
queue: [] as Song[],
|
||||||
theme: 'dark' as 'dark' | 'light',
|
theme: 'dark' as 'dark' | 'light',
|
||||||
|
playStartedAt: 0, // timestamp when current song started
|
||||||
|
pausedElapsed: 0, // elapsed time when paused
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
@@ -44,6 +46,13 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
isPaused(): boolean {
|
isPaused(): boolean {
|
||||||
return this.activeBot?.paused ?? false;
|
return this.activeBot?.paused ?? false;
|
||||||
},
|
},
|
||||||
|
/** Elapsed playback time in seconds */
|
||||||
|
elapsed(): number {
|
||||||
|
if (!this.activeBot?.currentSong) return 0;
|
||||||
|
if (this.isPaused) return this.pausedElapsed;
|
||||||
|
if (!this.isPlaying || this.playStartedAt === 0) return 0;
|
||||||
|
return (Date.now() - this.playStartedAt) / 1000;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
@@ -52,12 +61,34 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
updateBotStatus(botId: string, status: BotStatus) {
|
updateBotStatus(botId: string, status: BotStatus) {
|
||||||
|
const prev = this.bots.find((b) => b.id === botId);
|
||||||
|
const prevSongId = prev?.currentSong?.id;
|
||||||
|
const newSongId = status.currentSong?.id;
|
||||||
|
|
||||||
const index = this.bots.findIndex((b) => b.id === botId);
|
const index = this.bots.findIndex((b) => b.id === botId);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
this.bots[index] = status;
|
this.bots[index] = status;
|
||||||
} else {
|
} else {
|
||||||
this.bots.push(status);
|
this.bots.push(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset play timer when song changes
|
||||||
|
if (newSongId && newSongId !== prevSongId) {
|
||||||
|
this.playStartedAt = Date.now();
|
||||||
|
this.pausedElapsed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track pause/resume
|
||||||
|
if (status.playing && !status.paused && prev?.paused) {
|
||||||
|
// Resumed — adjust start time
|
||||||
|
this.playStartedAt = Date.now() - this.pausedElapsed * 1000;
|
||||||
|
}
|
||||||
|
if (status.paused && !prev?.paused) {
|
||||||
|
// Paused — save elapsed
|
||||||
|
this.pausedElapsed = this.playStartedAt > 0
|
||||||
|
? (Date.now() - this.playStartedAt) / 1000
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
removeBotStatus(botId: string) {
|
removeBotStatus(botId: string) {
|
||||||
@@ -84,11 +115,28 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
if (!this.activeBotId && this.bots.length > 0) {
|
if (!this.activeBotId && this.bots.length > 0) {
|
||||||
this.activeBotId = this.bots[0].id;
|
this.activeBotId = this.bots[0].id;
|
||||||
}
|
}
|
||||||
|
// If a song is playing, set the start time
|
||||||
|
const bot = this.activeBot;
|
||||||
|
if (bot?.playing && bot.currentSong && this.playStartedAt === 0) {
|
||||||
|
this.playStartedAt = Date.now();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async fetchQueue() {
|
||||||
|
if (!this.activeBotId) return;
|
||||||
|
try {
|
||||||
|
const res = await axios.get(`/api/player/${this.activeBotId}/queue`);
|
||||||
|
this.queue = res.data.queue ?? [];
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async play(query: string, platform = 'netease') {
|
async play(query: string, platform = 'netease') {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
await axios.post(`/api/player/${this.activeBotId}/play`, { query, platform });
|
await axios.post(`/api/player/${this.activeBotId}/play`, { query, platform });
|
||||||
|
this.playStartedAt = Date.now();
|
||||||
|
this.pausedElapsed = 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
async addToQueue(query: string, platform = 'netease') {
|
async addToQueue(query: string, platform = 'netease') {
|
||||||
@@ -98,27 +146,37 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
|
|
||||||
async pause() {
|
async pause() {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
|
this.pausedElapsed = this.playStartedAt > 0
|
||||||
|
? (Date.now() - this.playStartedAt) / 1000
|
||||||
|
: 0;
|
||||||
await axios.post(`/api/player/${this.activeBotId}/pause`);
|
await axios.post(`/api/player/${this.activeBotId}/pause`);
|
||||||
},
|
},
|
||||||
|
|
||||||
async resume() {
|
async resume() {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
|
this.playStartedAt = Date.now() - this.pausedElapsed * 1000;
|
||||||
await axios.post(`/api/player/${this.activeBotId}/resume`);
|
await axios.post(`/api/player/${this.activeBotId}/resume`);
|
||||||
},
|
},
|
||||||
|
|
||||||
async next() {
|
async next() {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
await axios.post(`/api/player/${this.activeBotId}/next`);
|
await axios.post(`/api/player/${this.activeBotId}/next`);
|
||||||
|
this.playStartedAt = Date.now();
|
||||||
|
this.pausedElapsed = 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
async prev() {
|
async prev() {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
await axios.post(`/api/player/${this.activeBotId}/prev`);
|
await axios.post(`/api/player/${this.activeBotId}/prev`);
|
||||||
|
this.playStartedAt = Date.now();
|
||||||
|
this.pausedElapsed = 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
async stop() {
|
async stop() {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
await axios.post(`/api/player/${this.activeBotId}/stop`);
|
await axios.post(`/api/player/${this.activeBotId}/stop`);
|
||||||
|
this.playStartedAt = 0;
|
||||||
|
this.pausedElapsed = 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
async setVolume(volume: number) {
|
async setVolume(volume: number) {
|
||||||
|
|||||||
+122
-16
@@ -15,14 +15,19 @@
|
|||||||
<div v-if="loading" class="lyrics-loading">加载歌词中...</div>
|
<div v-if="loading" class="lyrics-loading">加载歌词中...</div>
|
||||||
<div v-else-if="lines.length === 0" class="lyrics-empty">暂无歌词</div>
|
<div v-else-if="lines.length === 0" class="lyrics-empty">暂无歌词</div>
|
||||||
<div v-else class="lyrics-scroll" ref="scrollContainer">
|
<div v-else class="lyrics-scroll" ref="scrollContainer">
|
||||||
|
<div class="lyrics-spacer" />
|
||||||
<div
|
<div
|
||||||
v-for="(line, i) in lines"
|
v-for="(line, i) in lines"
|
||||||
:key="i"
|
:key="i"
|
||||||
|
:ref="el => { if (el) lineRefs[i] = el as HTMLElement }"
|
||||||
class="lyrics-line"
|
class="lyrics-line"
|
||||||
:class="{ active: i === activeLine }"
|
:class="{ active: i === activeLine }"
|
||||||
|
@click="seekToLine(i)"
|
||||||
>
|
>
|
||||||
{{ line.text }}
|
<div class="lyrics-text">{{ line.text }}</div>
|
||||||
|
<div v-if="line.translation" class="lyrics-translation">{{ line.translation }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="lyrics-spacer" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -34,7 +39,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, watch, onMounted } from 'vue';
|
import { ref, computed, watch, onMounted, onUnmounted } from 'vue';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { usePlayerStore } from '../stores/player.js';
|
import { usePlayerStore } from '../stores/player.js';
|
||||||
import CoverArt from '../components/CoverArt.vue';
|
import CoverArt from '../components/CoverArt.vue';
|
||||||
@@ -45,13 +50,15 @@ const currentSong = computed(() => store.currentSong);
|
|||||||
interface LyricLine {
|
interface LyricLine {
|
||||||
time: number;
|
time: number;
|
||||||
text: string;
|
text: string;
|
||||||
|
translation?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lines = ref<LyricLine[]>([]);
|
const lines = ref<LyricLine[]>([]);
|
||||||
const activeLine = ref(0);
|
const activeLine = ref(-1);
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const scrollContainer = ref<HTMLElement | null>(null);
|
const scrollContainer = ref<HTMLElement | null>(null);
|
||||||
const dominantColor = ref('');
|
const lineRefs = ref<Record<number, HTMLElement>>({});
|
||||||
|
let syncTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
|
||||||
const bgStyle = computed(() => {
|
const bgStyle = computed(() => {
|
||||||
if (currentSong.value?.coverUrl) {
|
if (currentSong.value?.coverUrl) {
|
||||||
@@ -68,6 +75,7 @@ async function fetchLyrics() {
|
|||||||
if (!currentSong.value) return;
|
if (!currentSong.value) return;
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
lines.value = [];
|
lines.value = [];
|
||||||
|
activeLine.value = -1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await axios.get(`/api/music/lyrics/${currentSong.value.id}`, {
|
const res = await axios.get(`/api/music/lyrics/${currentSong.value.id}`, {
|
||||||
@@ -81,12 +89,71 @@ async function fetchLyrics() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findActiveLine(elapsed: number): number {
|
||||||
|
if (lines.value.length === 0) return -1;
|
||||||
|
// Find the last line whose time <= elapsed
|
||||||
|
let idx = -1;
|
||||||
|
for (let i = 0; i < lines.value.length; i++) {
|
||||||
|
if (lines.value[i].time <= elapsed) {
|
||||||
|
idx = i;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
function scrollToActiveLine(idx: number) {
|
||||||
|
const el = lineRefs.value[idx];
|
||||||
|
if (el && scrollContainer.value) {
|
||||||
|
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncLyrics() {
|
||||||
|
if (!store.isPlaying || lines.value.length === 0) return;
|
||||||
|
const elapsed = store.elapsed;
|
||||||
|
const idx = findActiveLine(elapsed);
|
||||||
|
if (idx !== activeLine.value && idx >= 0) {
|
||||||
|
activeLine.value = idx;
|
||||||
|
scrollToActiveLine(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function seekToLine(index: number) {
|
||||||
|
// Can't actually seek server-side playback, just highlight
|
||||||
|
activeLine.value = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
function startSync() {
|
||||||
|
stopSync();
|
||||||
|
syncTimer = setInterval(syncLyrics, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopSync() {
|
||||||
|
if (syncTimer) {
|
||||||
|
clearInterval(syncTimer);
|
||||||
|
syncTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
watch(currentSong, () => {
|
watch(currentSong, () => {
|
||||||
fetchLyrics();
|
fetchLyrics();
|
||||||
|
lineRefs.value = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
watch(() => store.isPlaying, (playing) => {
|
||||||
|
if (playing) startSync();
|
||||||
|
else stopSync();
|
||||||
|
}, { immediate: true });
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
if (currentSong.value) fetchLyrics();
|
if (currentSong.value) fetchLyrics();
|
||||||
|
if (store.isPlaying) startSync();
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
stopSync();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -103,7 +170,7 @@ onMounted(() => {
|
|||||||
.lyrics-overlay {
|
.lyrics-overlay {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
inset: 0;
|
inset: 0;
|
||||||
background: rgba(0, 0, 0, 0.7);
|
background: rgba(0, 0, 0, 0.75);
|
||||||
backdrop-filter: blur(60px);
|
backdrop-filter: blur(60px);
|
||||||
-webkit-backdrop-filter: blur(60px);
|
-webkit-backdrop-filter: blur(60px);
|
||||||
}
|
}
|
||||||
@@ -116,6 +183,7 @@ onMounted(() => {
|
|||||||
max-width: 1000px;
|
max-width: 1000px;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 40px;
|
padding: 40px;
|
||||||
|
height: 80vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
.lyrics-left {
|
.lyrics-left {
|
||||||
@@ -123,6 +191,7 @@ onMounted(() => {
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 24px;
|
gap: 24px;
|
||||||
|
flex-shrink: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.song-meta {
|
.song-meta {
|
||||||
@@ -143,31 +212,68 @@ onMounted(() => {
|
|||||||
|
|
||||||
.lyrics-right {
|
.lyrics-right {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
max-height: 400px;
|
overflow: hidden;
|
||||||
overflow-y: auto;
|
position: relative;
|
||||||
|
mask-image: linear-gradient(
|
||||||
|
transparent 0%,
|
||||||
|
black 15%,
|
||||||
|
black 85%,
|
||||||
|
transparent 100%
|
||||||
|
);
|
||||||
|
-webkit-mask-image: linear-gradient(
|
||||||
|
transparent 0%,
|
||||||
|
black 15%,
|
||||||
|
black 85%,
|
||||||
|
transparent 100%
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
.lyrics-scroll {
|
.lyrics-scroll {
|
||||||
display: flex;
|
height: 100%;
|
||||||
flex-direction: column;
|
overflow-y: auto;
|
||||||
gap: 16px;
|
scrollbar-width: none;
|
||||||
padding: 40px 0;
|
&::-webkit-scrollbar { display: none; }
|
||||||
|
}
|
||||||
|
|
||||||
|
.lyrics-spacer {
|
||||||
|
height: 40%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.lyrics-line {
|
.lyrics-line {
|
||||||
font-size: 18px;
|
padding: 8px 0;
|
||||||
color: rgba(255, 255, 255, 0.35);
|
|
||||||
transition: all 0.3s ease;
|
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
transition: all 0.4s cubic-bezier(0.25, 0.1, 0.25, 1);
|
||||||
|
|
||||||
|
.lyrics-text {
|
||||||
|
font-size: 18px;
|
||||||
|
line-height: 1.5;
|
||||||
|
color: rgba(255, 255, 255, 0.3);
|
||||||
|
transition: all 0.4s cubic-bezier(0.25, 0.1, 0.25, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.lyrics-translation {
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.4;
|
||||||
|
color: rgba(255, 255, 255, 0.15);
|
||||||
|
margin-top: 2px;
|
||||||
|
transition: all 0.4s cubic-bezier(0.25, 0.1, 0.25, 1);
|
||||||
|
}
|
||||||
|
|
||||||
&.active {
|
&.active {
|
||||||
|
.lyrics-text {
|
||||||
font-size: 22px;
|
font-size: 22px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
.lyrics-translation {
|
||||||
|
color: rgba(255, 255, 255, 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
&:hover {
|
&:hover:not(.active) {
|
||||||
color: rgba(255, 255, 255, 0.6);
|
.lyrics-text {
|
||||||
|
color: rgba(255, 255, 255, 0.5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user