mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-09-03 04:03:46 +08:00
完善音乐模块的稳定性
This commit is contained in:
@@ -39,4 +39,4 @@ export class AudioEffect extends AudioSource {
|
||||
if (this.node) this.node.off(AudioSource.EventType.ENDED, this.onAudioEnded, this);
|
||||
this.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,10 @@ export class AudioEffectPool {
|
||||
*/
|
||||
getSwitch(type: string = AudioEffectType.Effect) {
|
||||
const iad = this.data[type];
|
||||
if (iad == null) console.error(`类型为【${type}】的音效配置不存在`);
|
||||
if (iad == null) {
|
||||
console.error(`类型为【${type}】的音效配置不存在`);
|
||||
return false;
|
||||
}
|
||||
return iad.switch;
|
||||
}
|
||||
/**
|
||||
@@ -55,7 +58,10 @@ export class AudioEffectPool {
|
||||
*/
|
||||
setSwitch(value: boolean, type: string = AudioEffectType.Effect) {
|
||||
const iad = this.data[type];
|
||||
if (iad == null) console.error(`类型为【${type}】的音效配置不存在`);
|
||||
if (iad == null) {
|
||||
console.error(`类型为【${type}】的音效配置不存在`);
|
||||
return;
|
||||
}
|
||||
iad.switch = value;
|
||||
|
||||
if (!value) this.stop();
|
||||
@@ -68,7 +74,10 @@ export class AudioEffectPool {
|
||||
*/
|
||||
getVolume(type: string = AudioEffectType.Effect) {
|
||||
const iad = this.data[type];
|
||||
if (iad == null) console.error(`类型为【${type}】的音效配置不存在`);
|
||||
if (iad == null) {
|
||||
console.error(`类型为【${type}】的音效配置不存在`);
|
||||
return 0;
|
||||
}
|
||||
return iad.volume;
|
||||
}
|
||||
/**
|
||||
@@ -78,7 +87,10 @@ export class AudioEffectPool {
|
||||
*/
|
||||
setVolume(value: number, type: string = AudioEffectType.Effect) {
|
||||
const iad = this.data[type];
|
||||
if (iad == null) console.error(`类型为【${type}】的音效配置不存在`);
|
||||
if (iad == null) {
|
||||
console.error(`类型为【${type}】的音效配置不存在`);
|
||||
return;
|
||||
}
|
||||
iad.volume = value;
|
||||
|
||||
this.effects.forEach(ac => (ac.volume = value));
|
||||
@@ -128,7 +140,8 @@ export class AudioEffectPool {
|
||||
node = new Node('AudioEffect');
|
||||
ae = node.addComponent(AudioEffect)!;
|
||||
ae.onComplete = this.onAudioEffectPlayComplete.bind(this);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
node = this.pool.get()!;
|
||||
ae = node.getComponent(AudioEffect)!;
|
||||
}
|
||||
@@ -149,7 +162,8 @@ export class AudioEffectPool {
|
||||
ae.play();
|
||||
|
||||
return ae;
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e) {
|
||||
// 播放异常时清理 effects 条目,防止残留
|
||||
this.effects.delete(ae.key);
|
||||
this.put(ae);
|
||||
@@ -199,7 +213,8 @@ export class AudioEffectPool {
|
||||
this.effects.forEach((ae, key) => {
|
||||
if (ae.state === AudioSource.AudioState.PAUSED) {
|
||||
ae.play();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// 已播完(STOPPED/INIT),不再恢复,记录待清理
|
||||
finishedKeys.push(key);
|
||||
}
|
||||
@@ -248,7 +263,8 @@ export class AudioEffectPool {
|
||||
if (node) {
|
||||
node.destroy();
|
||||
destroyed++;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -258,15 +274,15 @@ export class AudioEffectPool {
|
||||
private mergeParams(params?: IAudioParams): IAudioParams {
|
||||
return params
|
||||
? {
|
||||
type: params.type ?? AudioEffectType.Effect,
|
||||
loop: params.loop ?? false,
|
||||
volume: params.volume,
|
||||
path: params.path,
|
||||
onPlayComplete: params.onPlayComplete,
|
||||
}
|
||||
type: params.type ?? AudioEffectType.Effect,
|
||||
loop: params.loop ?? false,
|
||||
volume: params.volume,
|
||||
path: params.path,
|
||||
onPlayComplete: params.onPlayComplete,
|
||||
}
|
||||
: {
|
||||
type: AudioEffectType.Effect,
|
||||
loop: false,
|
||||
};
|
||||
type: AudioEffectType.Effect,
|
||||
loop: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ export class AudioManager extends Component {
|
||||
* @param params 音效参数
|
||||
*/
|
||||
playMusic(clip: AudioClip, params?: IAudioParams) {
|
||||
this.music.play(clip, params);
|
||||
this.music?.play(clip, params);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,30 +38,30 @@ export class AudioManager extends Component {
|
||||
* @param params 音效参数
|
||||
*/
|
||||
playEffect(clip: AudioClip, params?: IAudioParams): AudioEffect | null {
|
||||
return this.effect.play(clip, params);
|
||||
return this.effect?.play(clip, params) ?? null;
|
||||
}
|
||||
|
||||
/** 回收音效播放器 */
|
||||
putEffect(ae: AudioEffect) {
|
||||
this.effect.put(ae);
|
||||
this.effect?.put(ae);
|
||||
}
|
||||
|
||||
/** 恢复当前暂停的音乐与音效播放 */
|
||||
resumeAll() {
|
||||
this.music.resume();
|
||||
this.effect.resume();
|
||||
this.music?.resume();
|
||||
this.effect?.resume();
|
||||
}
|
||||
|
||||
/** 暂停当前音乐与音效的播放 */
|
||||
pauseAll() {
|
||||
this.music.pause();
|
||||
this.effect.pause();
|
||||
this.music?.pause();
|
||||
this.effect?.pause();
|
||||
}
|
||||
|
||||
/** 停止当前音乐与音效的播放 */
|
||||
stopAll() {
|
||||
this.music.stop();
|
||||
this.effect.stop();
|
||||
this.music?.stop();
|
||||
this.effect?.stop();
|
||||
}
|
||||
|
||||
/** 保存音乐音效的音量、开关配置数据到本地 */
|
||||
|
||||
@@ -49,12 +49,12 @@ export class AudioMusic extends Node {
|
||||
*/
|
||||
setVolume(value: number) {
|
||||
this.data[AudioEffectType.Music].volume = value;
|
||||
this._ae.volume = value;
|
||||
if (this._ae) this._ae.volume = value;
|
||||
}
|
||||
|
||||
/** 获取音乐播放进度 */
|
||||
get progress(): number {
|
||||
if (this._ae.duration > 0) this._progress = this._ae.currentTime / this._ae.duration;
|
||||
if (this._ae && this._ae.duration > 0) this._progress = this._ae.currentTime / this._ae.duration;
|
||||
return this._progress;
|
||||
}
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ export class AudioMusic extends Node {
|
||||
*/
|
||||
set progress(value: number) {
|
||||
this._progress = value;
|
||||
this._ae.currentTime = value * this._ae.duration;
|
||||
if (this._ae) this._ae.currentTime = value * this._ae.duration;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
@@ -85,6 +85,7 @@ export class AudioMusic extends Node {
|
||||
*/
|
||||
play(clip: AudioClip, params?: IAudioParams) {
|
||||
if (!this.getSwitch()) return;
|
||||
if (!this._ae) return;
|
||||
|
||||
if (this._ae.playing) this.stop();
|
||||
|
||||
@@ -98,17 +99,17 @@ export class AudioMusic extends Node {
|
||||
|
||||
/** 恢复当前暂停的音乐与音效播放 */
|
||||
resume() {
|
||||
if (!this._ae.playing && this.progress > 0) this._ae.play();
|
||||
if (this._ae && !this._ae.playing && this.progress > 0) this._ae.play();
|
||||
}
|
||||
|
||||
/** 暂停当前音乐与音效的播放 */
|
||||
pause() {
|
||||
if (this._ae.playing) this._ae.pause();
|
||||
if (this._ae && this._ae.playing) this._ae.pause();
|
||||
}
|
||||
|
||||
/** 停止当前音乐与音效的播放 */
|
||||
stop(): void {
|
||||
if (this._ae.playing) this._ae.stop();
|
||||
if (this._ae && this._ae.playing) this._ae.stop();
|
||||
}
|
||||
|
||||
/** 节点销毁时清理 */
|
||||
|
||||
Reference in New Issue
Block a user