Commit 80a8a613 authored by Jeff nan's avatar Jeff nan

debug

parent c2b2cd35
...@@ -40,8 +40,6 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges { ...@@ -40,8 +40,6 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges {
wrongAudio = new Audio(); wrongAudio = new Audio();
_lc_text = ''; _lc_text = '';
_data = []; _data = [];
hasUnlockedAudio = false;
sfxActive = new Set<HTMLAudioElement>();
constructor(private appRef: ApplicationRef, constructor(private appRef: ApplicationRef,
public changeDetectorRef: ChangeDetectorRef) { public changeDetectorRef: ChangeDetectorRef) {
...@@ -125,55 +123,13 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges { ...@@ -125,55 +123,13 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges {
this.refresh(); this.refresh();
} }
async ensureAudioUnlocked(): Promise<void> { handleTap(ev, item) {
if (this.hasUnlockedAudio) {
return;
}
try {
this.rightAudio.muted = true;
this.rightAudio.currentTime = 0;
await this.rightAudio.play();
this.rightAudio.pause();
this.rightAudio.muted = false;
this.rightAudio.currentTime = 0;
this.hasUnlockedAudio = true;
} catch (e) {
// 忽略,后续播放时再尝试
}
}
async playSfx(base: HTMLAudioElement): Promise<void> {
const clip = base.cloneNode(true) as HTMLAudioElement;
clip.preload = 'auto';
clip.currentTime = 0;
this.sfxActive.add(clip);
const cleanup = () => {
this.sfxActive.delete(clip);
clip.src = '';
};
clip.addEventListener('ended', cleanup, { once: true });
clip.addEventListener('error', cleanup, { once: true });
try {
await clip.play();
} catch (err) {
if ((err as any)?.name === 'NotAllowedError' && !this.hasUnlockedAudio) {
await this.ensureAudioUnlocked();
try {
await clip.play();
return;
} catch (_) {}
}
cleanup();
}
}
async handleTap(ev, item) {
if (item.is_right) { if (item.is_right) {
item.state = 'right'; item.state = 'right';
await this.playSfx(this.rightAudio); new Audio(this.rightAudio.src + '?v=' + new Date().getTime()).play();
} else { } else {
item.state = 'wrong'; item.state = 'wrong';
await this.playSfx(this.wrongAudio); new Audio(this.wrongAudio.src + '?v=' + new Date().getTime()).play();
setTimeout(() => { setTimeout(() => {
item.state = ''; item.state = '';
}, 1500); }, 1500);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment