feat: 优化播放

This commit is contained in:
alger
2025-01-17 22:34:49 +08:00
parent 573023600a
commit a94e0efba5
3 changed files with 54 additions and 32 deletions
+15 -4
View File
@@ -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) => {
let retryCount = 0;
const maxRetries = 3;
@@ -131,10 +135,10 @@ class AudioService {
this.currentSound.unload();
}
this.currentSound = null;
this.currentTrack = track;
this.currentTrack = track as SongResult;
this.currentSound = new Howl({
src: [url],
src: [url as string],
html5: true,
autoplay: true,
volume: localStorage.getItem('volume')
@@ -163,7 +167,7 @@ class AudioService {
});
// 更新媒体会话元数据
this.updateMediaSessionMetadata(track);
this.updateMediaSessionMetadata(track as SongResult);
// 设置音频事件监听
this.currentSound.on('play', () => {
@@ -230,6 +234,13 @@ class AudioService {
}
}
pause() {
if (this.currentSound) {
this.currentSound.pause();
}
}
clearAllListeners() {
this.callbacks = {};
}