音效播放

This commit is contained in:
unanmed 2023-06-21 17:55:52 +08:00
parent 85f9a9fd07
commit bb54531a0e
2 changed files with 20 additions and 49 deletions

View File

@ -3614,62 +3614,19 @@ control.prototype.playSound = function (sound, pitch, callback) {
!core.material.sounds[sound] !core.material.sounds[sound]
) )
return; return;
try { ancTe.sound.play(sound, callback);
if (core.musicStatus.audioContext != null) {
var source = core.musicStatus.audioContext.createBufferSource();
source.__name = sound;
source.buffer = core.material.sounds[sound];
source.connect(core.musicStatus.gainNode);
var id = setTimeout(null);
if (pitch && pitch >= 30 && pitch <= 300) {
source.playbackRate.setValueAtTime(pitch / 100, 0);
}
source.onended = function () {
delete core.musicStatus.playingSounds[id];
if (callback) callback();
};
core.musicStatus.playingSounds[id] = source;
if (source.start) source.start(0);
else if (source.noteOn) source.noteOn(0);
return id;
} else {
core.material.sounds[sound].volume = core.musicStatus.userVolume;
core.material.sounds[sound].play();
if (callback) callback();
}
} catch (e) {
console.log('无法播放SE ' + sound);
console.error(e);
}
}; };
////// 停止所有音频 ////// ////// 停止所有音频 //////
control.prototype.stopSound = function (id) { control.prototype.stopSound = function (id) {
if (id == null) { if (typeof id === 'number') ancTe.sound.stop(id);
Object.keys(core.musicStatus.playingSounds).forEach(function (id) { else ancTe.sound.stopAll();
core.control.stopSound(id);
});
return;
}
var source = core.musicStatus.playingSounds[id];
if (!source) return;
try {
if (source.stop) source.stop();
else if (source.noteOff) source.noteOff();
} catch (e) {
console.error(e);
}
delete core.musicStatus.playingSounds[id];
}; };
////// 获得当前正在播放的所有指定音效的id列表 ////// ////// 获得当前正在播放的所有指定音效的id列表 //////
control.prototype.getPlayingSounds = function (name) { control.prototype.getPlayingSounds = function (name) {
name = core.getMappedName(name); name = core.getMappedName(name);
return Object.keys(core.musicStatus.playingSounds).filter(function (one) { return ancTe.sound.getPlaying(name);
return (
name == null || core.musicStatus.playingSounds[one].__name == name
);
});
}; };
////// 检查bgm状态 ////// ////// 检查bgm状态 //////

View File

@ -95,6 +95,7 @@ export class SoundEffect extends AudioPlayer {
const index = SoundEffect.playIndex++; const index = SoundEffect.playIndex++;
this.playing[index] = node; this.playing[index] = node;
this.playMap.set(node, index); this.playMap.set(node, index);
return index; return index;
} }
@ -136,7 +137,7 @@ export class SoundController extends ResourceController<
ArrayBuffer, ArrayBuffer,
SoundEffect SoundEffect
> { > {
private seIndex: Record<number, SoundEffect> = {}; private seIndex: Record<string, SoundEffect> = {};
/** /**
* *
@ -157,11 +158,13 @@ export class SoundController extends ResourceController<
* @param sound * @param sound
* @returns -1 * @returns -1
*/ */
play(sound: SoundIds): number { play(sound: SoundIds, end?: () => void): number {
const se = this.get(sound); const se = this.get(sound);
const index = se.playSE(); const index = se.playSE();
if (!has(index)) return -1; if (!has(index)) return -1;
this.seIndex[index] = se; this.seIndex[index] = se;
if (end) se.once('end', end);
se.volumn = core.musicStatus.userVolume;
return index; return index;
} }
@ -198,4 +201,15 @@ export class SoundController extends ResourceController<
get(sound: SoundIds) { get(sound: SoundIds) {
return this.list[`sounds.${sound}`]; return this.list[`sounds.${sound}`];
} }
getPlaying(sound?: SoundIds) {
if (sound) {
const se = this.get(sound);
return Object.keys(this.seIndex).filter(
v => this.seIndex[v] === se
);
} else {
return Object.keys(this.seIndex);
}
}
} }