Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/webgal/src/Core/gameScripts/vocal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ export const playVocal = (sentence: ISentence) => {
audioContextWrapper.dataArray = new Uint8Array(bufferLength);
let vocalControl = document.getElementById('currentVocal') as HTMLMediaElement;

if (!audioContextWrapper.source) {
if (!audioContextWrapper.source || audioContextWrapper.source.mediaElement !== vocalControl) {
if (audioContextWrapper.source) {
audioContextWrapper.source.disconnect();
}
audioContextWrapper.source = audioContextWrapper.audioContext.createMediaElementSource(vocalControl);
audioContextWrapper.source.connect(audioContextWrapper.analyser);
audioContextWrapper.source.connect(audioContextWrapper.analyser!);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了代码更清晰并避免使用非空断言 !,建议使用在函数前面已定义的 analyser 常量。这样可以利用 TypeScript 的类型推断,使代码更安全、更易读。

Suggested change
audioContextWrapper.source.connect(audioContextWrapper.analyser!);
audioContextWrapper.source.connect(analyser);

}

audioContextWrapper.analyser.connect(audioContextWrapper.audioContext.destination);
Expand Down