mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-03 14:20:50 +08:00
Update audioService.ts
This commit is contained in:
@@ -330,15 +330,17 @@ class AudioService {
|
|||||||
// 应用EQ状态
|
// 应用EQ状态
|
||||||
this.applyBypassState();
|
this.applyBypassState();
|
||||||
|
|
||||||
// 设置音量
|
// 从 localStorage 应用音量到增益节点
|
||||||
const volume = localStorage.getItem('volume');
|
const savedVolume = localStorage.getItem('volume');
|
||||||
if (this.gainNode) {
|
if (savedVolume) {
|
||||||
this.gainNode.gain.value = volume ? parseFloat(volume) : 1;
|
this.applyVolume(parseFloat(savedVolume));
|
||||||
|
} else {
|
||||||
|
this.applyVolume(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('EQ初始化成功');
|
console.log('EQ initialization successful');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('EQ初始化失败:', error);
|
console.error('EQ initialization failed:', error);
|
||||||
await this.disposeEQ();
|
await this.disposeEQ();
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -456,7 +458,7 @@ class AudioService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 播放控制相关
|
// 播放控制相关
|
||||||
play(url?: string, track?: SongResult, isPlay: boolean = true, seekTime: number = 0): Promise<Howl> {
|
play(url?: string, track?: SongResult, isPlay: boolean = true): Promise<Howl> {
|
||||||
// 每次调用play方法时,尝试强制重置锁(注意:仅在页面刷新后的第一次播放时应用)
|
// 每次调用play方法时,尝试强制重置锁(注意:仅在页面刷新后的第一次播放时应用)
|
||||||
if (!this.currentSound) {
|
if (!this.currentSound) {
|
||||||
console.log('首次播放请求,强制重置操作锁');
|
console.log('首次播放请求,强制重置操作锁');
|
||||||
@@ -563,11 +565,9 @@ class AudioService {
|
|||||||
this.currentSound = new Howl({
|
this.currentSound = new Howl({
|
||||||
src: [url],
|
src: [url],
|
||||||
html5: true,
|
html5: true,
|
||||||
autoplay: false, // 修改为 false,不自动播放,等待完全初始化后手动播放
|
autoplay: false,
|
||||||
volume: localStorage.getItem('volume')
|
volume: 1, // 禁用 Howler.js 音量控制
|
||||||
? parseFloat(localStorage.getItem('volume') as string)
|
rate: this.playbackRate,
|
||||||
: 1,
|
|
||||||
rate: this.playbackRate, // 设置初始播放速度
|
|
||||||
format: ['mp3', 'aac'],
|
format: ['mp3', 'aac'],
|
||||||
onloaderror: (_, error) => {
|
onloaderror: (_, error) => {
|
||||||
console.error('Audio load error:', error);
|
console.error('Audio load error:', error);
|
||||||
@@ -596,14 +596,19 @@ class AudioService {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onload: async () => {
|
onload: async () => {
|
||||||
// 音频加载成功后设置 EQ 和更新媒体会话
|
try {
|
||||||
if (this.currentSound) {
|
// 初始化音频管道
|
||||||
try {
|
await this.setupEQ(this.currentSound!);
|
||||||
if (seekTime > 0) {
|
|
||||||
this.currentSound.seek(seekTime);
|
// 重新应用已保存的音量
|
||||||
}
|
const savedVolume = localStorage.getItem('volume');
|
||||||
|
if (savedVolume) {
|
||||||
|
this.applyVolume(parseFloat(savedVolume));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 音频加载成功后设置 EQ 和更新媒体会话
|
||||||
|
if (this.currentSound) {
|
||||||
console.log('audioService: 音频加载成功,设置 EQ');
|
console.log('audioService: 音频加载成功,设置 EQ');
|
||||||
await this.setupEQ(this.currentSound);
|
|
||||||
this.updateMediaSessionMetadata(track);
|
this.updateMediaSessionMetadata(track);
|
||||||
this.updateMediaSessionPositionState();
|
this.updateMediaSessionPositionState();
|
||||||
this.emit('load');
|
this.emit('load');
|
||||||
@@ -615,15 +620,11 @@ class AudioService {
|
|||||||
this.currentSound.play();
|
this.currentSound.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(this.currentSound);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('设置 EQ 失败:', error);
|
|
||||||
// 即使 EQ 设置失败,也继续播放(如果需要)
|
|
||||||
if (isPlay) {
|
|
||||||
this.currentSound.play();
|
|
||||||
}
|
|
||||||
resolve(this.currentSound);
|
resolve(this.currentSound);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Audio initialization failed:', error);
|
||||||
|
reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -702,10 +703,7 @@ class AudioService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setVolume(volume: number) {
|
setVolume(volume: number) {
|
||||||
if (this.currentSound) {
|
this.applyVolume(volume);
|
||||||
this.currentSound.volume(volume);
|
|
||||||
localStorage.setItem('volume', volume.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
seek(time: number) {
|
seek(time: number) {
|
||||||
@@ -782,6 +780,27 @@ class AudioService {
|
|||||||
public getPlaybackRate(): number {
|
public getPlaybackRate(): number {
|
||||||
return this.playbackRate;
|
return this.playbackRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 新的音量调节方法
|
||||||
|
private applyVolume(volume: number) {
|
||||||
|
// 确保值在0到1之间
|
||||||
|
const normalizedVolume = Math.max(0, Math.min(1, volume));
|
||||||
|
|
||||||
|
// 使用线性缩放音量
|
||||||
|
const linearVolume = normalizedVolume;
|
||||||
|
|
||||||
|
// 将音量应用到所有相关节点
|
||||||
|
if (this.gainNode) {
|
||||||
|
// 立即设置音量
|
||||||
|
this.gainNode.gain.cancelScheduledValues(this.context!.currentTime);
|
||||||
|
this.gainNode.gain.setValueAtTime(linearVolume, this.context!.currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存值
|
||||||
|
localStorage.setItem('volume', linearVolume.toString());
|
||||||
|
|
||||||
|
console.log('Volume applied (linear):', linearVolume);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const audioService = new AudioService();
|
export const audioService = new AudioService();
|
||||||
|
|||||||
Reference in New Issue
Block a user