mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-28 19:07:23 +08:00
✨ feat: 优化播放
This commit is contained in:
@@ -149,6 +149,30 @@ const setupAudioListeners = () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const replayMusic = async () => {
|
||||||
|
try {
|
||||||
|
// 如果当前有音频实例,先停止并销毁
|
||||||
|
if (sound.value) {
|
||||||
|
sound.value.stop();
|
||||||
|
sound.value.unload();
|
||||||
|
sound.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新播放当前歌曲
|
||||||
|
if (store.state.playMusicUrl && playMusic.value) {
|
||||||
|
const newSound = await audioService.play(store.state.playMusicUrl, playMusic.value);
|
||||||
|
sound.value = newSound as Howl;
|
||||||
|
setupAudioListeners();
|
||||||
|
} else {
|
||||||
|
console.error('No music URL or playMusic data available');
|
||||||
|
store.commit('nextPlay');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error replaying song:', error);
|
||||||
|
store.commit('nextPlay');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 监听结束
|
// 监听结束
|
||||||
audioService.on('end', () => {
|
audioService.on('end', () => {
|
||||||
clearInterval();
|
clearInterval();
|
||||||
@@ -156,23 +180,13 @@ const setupAudioListeners = () => {
|
|||||||
if (store.state.playMode === 1) {
|
if (store.state.playMode === 1) {
|
||||||
// 单曲循环模式
|
// 单曲循环模式
|
||||||
if (sound.value) {
|
if (sound.value) {
|
||||||
sound.value.seek(0);
|
replayMusic();
|
||||||
try {
|
|
||||||
sound.value.play();
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error replaying song:', error);
|
|
||||||
store.commit('nextPlay');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (store.state.playMode === 2) {
|
} else if (store.state.playMode === 2) {
|
||||||
// 随机播放模式
|
// 随机播放模式
|
||||||
const { playList } = store.state;
|
const { playList } = store.state;
|
||||||
if (playList.length <= 1) {
|
if (playList.length <= 1) {
|
||||||
try {
|
replayMusic();
|
||||||
sound.value?.play();
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error replaying song:', error);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let randomIndex;
|
let randomIndex;
|
||||||
do {
|
do {
|
||||||
@@ -466,13 +480,6 @@ if (isElectron) {
|
|||||||
|
|
||||||
// 在组件挂载时设置监听器
|
// 在组件挂载时设置监听器
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
const clearIntervalFn = setupAudioListeners();
|
setupAudioListeners();
|
||||||
useLyricProgress(); // 直接调用,不需要解构返回值
|
useLyricProgress(); // 直接调用,不需要解构返回值
|
||||||
|
|
||||||
// 在组件卸载时清理
|
|
||||||
onUnmounted(() => {
|
|
||||||
clearIntervalFn();
|
|
||||||
audioService.stop();
|
|
||||||
audioService.clearAllListeners();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ import {
|
|||||||
import type { SongResult } from '@/type/music';
|
import type { SongResult } from '@/type/music';
|
||||||
import { getImgUrl, isElectron, isMobile, secondToMinute, setAnimationClass } from '@/utils';
|
import { getImgUrl, isElectron, isMobile, secondToMinute, setAnimationClass } from '@/utils';
|
||||||
import { showShortcutToast } from '@/utils/shortcutToast';
|
import { showShortcutToast } from '@/utils/shortcutToast';
|
||||||
|
import { audioService } from '@/services/audioService';
|
||||||
|
|
||||||
import MusicFull from './MusicFull.vue';
|
import MusicFull from './MusicFull.vue';
|
||||||
|
|
||||||
@@ -282,16 +283,19 @@ const MusicFullRef = ref<any>(null);
|
|||||||
|
|
||||||
// 播放暂停按钮事件
|
// 播放暂停按钮事件
|
||||||
const playMusicEvent = async () => {
|
const playMusicEvent = async () => {
|
||||||
if (play.value) {
|
try {
|
||||||
if (sound.value) {
|
if (play.value) {
|
||||||
sound.value.pause();
|
audioService.pause();
|
||||||
|
store.commit('setPlayMusic', false);
|
||||||
|
} else {
|
||||||
|
audioService.play();
|
||||||
|
store.commit('setPlayMusic', true);
|
||||||
}
|
}
|
||||||
store.commit('setPlayMusic', false);
|
} catch (error) {
|
||||||
} else {
|
console.log('error',error)
|
||||||
if (sound.value) {
|
if (play.value) {
|
||||||
sound.value.play();
|
store.commit('nextPlay');
|
||||||
}
|
}
|
||||||
store.commit('setPlayMusic', true);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,11 @@ class AudioService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 播放控制相关
|
// 播放控制相关
|
||||||
play(url: string, track: SongResult): Promise<Howl> {
|
play(url?: string, track?: SongResult): Promise<Howl> {
|
||||||
|
if (this.currentSound && !url && !track) {
|
||||||
|
this.currentSound.play();
|
||||||
|
return Promise.resolve(this.currentSound as Howl);
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let retryCount = 0;
|
let retryCount = 0;
|
||||||
const maxRetries = 3;
|
const maxRetries = 3;
|
||||||
@@ -131,10 +135,10 @@ class AudioService {
|
|||||||
this.currentSound.unload();
|
this.currentSound.unload();
|
||||||
}
|
}
|
||||||
this.currentSound = null;
|
this.currentSound = null;
|
||||||
this.currentTrack = track;
|
this.currentTrack = track as SongResult;
|
||||||
|
|
||||||
this.currentSound = new Howl({
|
this.currentSound = new Howl({
|
||||||
src: [url],
|
src: [url as string],
|
||||||
html5: true,
|
html5: true,
|
||||||
autoplay: true,
|
autoplay: true,
|
||||||
volume: localStorage.getItem('volume')
|
volume: localStorage.getItem('volume')
|
||||||
@@ -163,7 +167,7 @@ class AudioService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 更新媒体会话元数据
|
// 更新媒体会话元数据
|
||||||
this.updateMediaSessionMetadata(track);
|
this.updateMediaSessionMetadata(track as SongResult);
|
||||||
|
|
||||||
// 设置音频事件监听
|
// 设置音频事件监听
|
||||||
this.currentSound.on('play', () => {
|
this.currentSound.on('play', () => {
|
||||||
@@ -230,6 +234,13 @@ class AudioService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pause() {
|
||||||
|
if (this.currentSound) {
|
||||||
|
this.currentSound.pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
clearAllListeners() {
|
clearAllListeners() {
|
||||||
this.callbacks = {};
|
this.callbacks = {};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user