|
|
@@ -358,6 +358,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
private readonly listeningPool = inject(ListeningPoolService);
|
|
|
private readonly wordAudio = inject(WordAudioService);
|
|
|
readonly audioPreparing = signal(false);
|
|
|
+ readonly pointReadAudio = signal<{ wordId: string; phase: 'loading' | 'playing'; since: number } | null>(null);
|
|
|
private audioRequest = 0;
|
|
|
private readonly wordPreviewPageSize = 30;
|
|
|
private examTimer: ReturnType<typeof setInterval> | null = null;
|
|
|
@@ -390,14 +391,13 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
- this.audioRequest++;
|
|
|
+ this.stopWordPlayback();
|
|
|
this.calendarRequestId++;this.calendarRequest?.unsubscribe();this.selectionRequest?.unsubscribe();
|
|
|
this.wordbookStudentsRequest?.unsubscribe();
|
|
|
this.choiceOptionsVersion++;
|
|
|
this.choiceOptionsRequest?.unsubscribe();
|
|
|
this.stopExamTimer();
|
|
|
this.examHistoryRequest?.unsubscribe();this.examLoadRequest?.unsubscribe();
|
|
|
- this.platform.stopAudio();
|
|
|
}
|
|
|
|
|
|
get isNodePage(): boolean {
|
|
|
@@ -637,10 +637,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
const previous = this.wordPreviewPage();
|
|
|
this.wordPreviewPage.set(target);
|
|
|
if (this.isHistoryWordView) {
|
|
|
- this.audioRequest++;
|
|
|
- this.platform.stopAudio();
|
|
|
- this.audioPreparing.set(false);
|
|
|
- this.historyAudioWordId.set('');
|
|
|
+ this.stopWordPlayback();
|
|
|
const start = (target - 1) * this.wordPreviewPageSize;
|
|
|
this.words.set(this.historyPreviewWords.slice(start, start + this.wordPreviewPageSize));
|
|
|
setTimeout(() => document.getElementById('readonly-word-title')?.scrollIntoView({ behavior: 'smooth', block: 'start' }));
|
|
|
@@ -768,7 +765,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
selectCoursePass(pass: number): void {
|
|
|
if (this.selectionLocked() || pass === this.coursePass() || pass < 0 || pass > 3) return;
|
|
|
- this.audioRequest++;this.platform.stopAudio();this.audioPreparing.set(false);
|
|
|
+ this.stopWordPlayback();
|
|
|
this.selectionRevealed.set(new Set());
|
|
|
this.bulkSelection.set(null);
|
|
|
this.courseSelectionNeedsRebase = false;
|
|
|
@@ -962,36 +959,62 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ private stopWordPlayback(): void {
|
|
|
+ this.audioRequest++;
|
|
|
+ this.platform.stopAudio();
|
|
|
+ this.audioPreparing.set(false);
|
|
|
+ this.historyAudioWordId.set('');
|
|
|
+ this.pointReadAudio.set(null);
|
|
|
+ }
|
|
|
+
|
|
|
async play(word = this.current()): Promise<void> {
|
|
|
if (this.isReadOnlySelectionPage || (this.isReadOnlyWordView && !this.isHistoryWordView) || !word) return;
|
|
|
- this.prepareWordAudio(word);
|
|
|
+ const selection = this.isRefineSelectionPage || this.isNewWordSelectionPage;
|
|
|
+ const pointReading = selection || this.isStudyListPage;
|
|
|
+ const pending = this.pointReadAudio();
|
|
|
+ // Study taps may replay once sound starts. On the selection list let the word finish first.
|
|
|
+ if (pointReading && pending?.wordId === word.id && (selection || pending.phase === 'loading')
|
|
|
+ && (this.audioPreparing() || Date.now() - pending.since < 8000)) return;
|
|
|
+ if (pointReading) this.pointReadAudio.set({ wordId: word.id, phase: 'loading', since: Date.now() });
|
|
|
const request = ++this.audioRequest, focusedWord = this.current(), step = this.studyStep();
|
|
|
const active = () => request === this.audioRequest && this.current() === focusedWord && this.studyStep() === step;
|
|
|
this.audioPreparing.set(false);
|
|
|
if (this.isHistoryWordView) this.historyAudioWordId.set(word.id);
|
|
|
+ const finishPlayback = () => {
|
|
|
+ this.historyAudioWordId.set('');
|
|
|
+ if (pointReading) this.pointReadAudio.set(null);
|
|
|
+ };
|
|
|
const playAudio = async (url: string, silentFailure = false) => {
|
|
|
- const status = await (this.isHistoryWordView || this.isSelectionPage || this.isStudyListPage
|
|
|
+ const playback = this.isHistoryWordView || this.isSelectionPage || this.isStudyListPage
|
|
|
? this.platform.play(url, silentFailure, {
|
|
|
onEnded: () => {
|
|
|
if (!active()) return;
|
|
|
if (this.isHistoryWordView) this.historyWordRevealed.update(current => new Set(current).add(word.id));
|
|
|
else if (!this.isStudyListPage) this.selectionRevealed.update(current => new Set(current).add(word.id));
|
|
|
- this.historyAudioWordId.set('');
|
|
|
+ finishPlayback();
|
|
|
},
|
|
|
onError: () => {
|
|
|
if (!active()) return;
|
|
|
- this.historyAudioWordId.set('');
|
|
|
+ finishPlayback();
|
|
|
this.platform.toast('音频播放中断,请再点一次单词重试', 'error');
|
|
|
},
|
|
|
})
|
|
|
- : this.platform.play(url, silentFailure));
|
|
|
- if (status !== 'played' && active()) this.historyAudioWordId.set('');
|
|
|
+ : this.platform.play(url, silentFailure);
|
|
|
+ this.prepareWordAudio(word);
|
|
|
+ const status = await playback;
|
|
|
+ if (active() && pointReading && status === 'played' && this.pointReadAudio()) {
|
|
|
+ this.pointReadAudio.set({ wordId: word.id, phase: 'playing', since: Date.now() });
|
|
|
+ }
|
|
|
return status;
|
|
|
};
|
|
|
const original = word.audio?.trim();
|
|
|
if (original) {
|
|
|
const status = await playAudio(this.wordAudioUrl(original), true);
|
|
|
- if (status !== 'failed' || !active()) return;
|
|
|
+ if (!active()) return;
|
|
|
+ if (status !== 'failed') {
|
|
|
+ if (status !== 'played') finishPlayback();
|
|
|
+ return;
|
|
|
+ }
|
|
|
} else this.platform.stopAudio();
|
|
|
this.audioPreparing.set(true);
|
|
|
if (this.isHistoryWordView) this.historyAudioWordId.set(word.id);
|
|
|
@@ -999,15 +1022,20 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
const url = await this.wordAudio.ensure(word.wordId, !!original);
|
|
|
if (!active()) return;
|
|
|
word.audio = url;
|
|
|
- if (original && this.wordAudioUrl(original) === this.wordAudioUrl(url)) {
|
|
|
- this.historyAudioWordId.set('');
|
|
|
+ if (pointReading) {
|
|
|
+ this.audioPreparing.set(false);
|
|
|
+ this.pointReadAudio.set({ wordId: word.id, phase: 'loading', since: Date.now() });
|
|
|
+ }
|
|
|
+ if (!pointReading && original && this.wordAudioUrl(original) === this.wordAudioUrl(url)) {
|
|
|
+ finishPlayback();
|
|
|
this.platform.toast('音频暂时无法播放,请检查网络后重试', 'error');
|
|
|
return;
|
|
|
}
|
|
|
- await playAudio(this.wordAudioUrl(url));
|
|
|
+ const status = await playAudio(this.wordAudioUrl(url));
|
|
|
+ if (active() && status !== 'played') finishPlayback();
|
|
|
} catch (error) {
|
|
|
if (active()) {
|
|
|
- this.historyAudioWordId.set('');
|
|
|
+ finishPlayback();
|
|
|
this.platform.toast(error instanceof Error ? error.message : '读音暂时不可用,请再点喇叭重试', 'error');
|
|
|
}
|
|
|
} finally {
|
|
|
@@ -1032,7 +1060,13 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
repeatWord(word: Word, index: number): void {
|
|
|
- this.studyMeaningRevealed.update(current => new Set([...current].filter(id => id !== word.id)));
|
|
|
+ if (this.studyMeaningRevealed().has(word.id)) {
|
|
|
+ this.studyMeaningRevealed.update(current => {
|
|
|
+ const revealed = new Set(current);
|
|
|
+ revealed.delete(word.id);
|
|
|
+ return revealed;
|
|
|
+ });
|
|
|
+ }
|
|
|
const count = this.repetitions()[word.id] ?? 0;
|
|
|
if (this.studyStep() === 1 && index > 0) {
|
|
|
const previous = this.words()[index - 1];
|
|
|
@@ -1051,9 +1085,10 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
shuffleStudyWords(): void {
|
|
|
- this.audioRequest++;this.platform.stopAudio();this.audioPreparing.set(false);
|
|
|
+ this.stopWordPlayback();
|
|
|
this.selectionRevealed.set(new Set());
|
|
|
this.words.set(shuffledStudy(this.words()));
|
|
|
+ this.prepareWordAudio(this.words()[0]);
|
|
|
this.repetitions.set({});
|
|
|
this.studyMeaningRevealed.set(new Set());
|
|
|
}
|
|
|
@@ -1103,8 +1138,8 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
return;
|
|
|
}
|
|
|
this.currentIndex.update((value) => value + 1);
|
|
|
+ void this.play();
|
|
|
this.prepareChoiceOptions();
|
|
|
- setTimeout(() => this.play(), 150);
|
|
|
}
|
|
|
|
|
|
spellLetter(index: number): void {
|
|
|
@@ -1549,7 +1584,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
selectNewWordBucket(bucket: 'pending' | 'completed'): void {
|
|
|
if (this.activeSelectionBucket() === bucket) return;
|
|
|
- this.audioRequest++;this.platform.stopAudio();this.audioPreparing.set(false);
|
|
|
+ this.stopWordPlayback();
|
|
|
this.selectionRequest?.unsubscribe();
|
|
|
this.activeSelectionBucket.set(bucket);
|
|
|
this.selectionPage.set(1);
|
|
|
@@ -1561,7 +1596,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
goToNewWordPage(page: number): void {
|
|
|
if (this.selectionLoading() || page < 1 || page > this.newWordPageCount() || page === this.selectionPage()) return;
|
|
|
- this.audioRequest++;this.platform.stopAudio();this.audioPreparing.set(false);
|
|
|
+ this.stopWordPlayback();
|
|
|
this.selectionPage.set(page);
|
|
|
if (this.legacyNewWords) { this.showLegacyNewWordPage(); return; }
|
|
|
this.loadSelection();
|
|
|
@@ -1576,6 +1611,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
this.newWordCounts.set({ pending: pending.length, completed: completed.length });
|
|
|
this.selectionTotal.set(bucket.length);
|
|
|
this.words.set(bucket.slice(start, start + this.newWordPageSize));
|
|
|
+ this.prepareWordAudio(this.words()[0]);
|
|
|
for (const word of all) this.newWordSelectionCache.set(word.id, word);
|
|
|
this.error.set('');
|
|
|
this.selectionLoading.set(false);
|
|
|
@@ -1617,6 +1653,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
const student = this.session.student() ?? this.session.user();this.selectionLoading.set(true);
|
|
|
this.selectionRequest=this.api.get<unknown>('e_ck_list',{checkwords:2,nids:this.query('nids')||this.query('nid'),pnid:this.query('pnid'),learningPass:this.coursePass(),modelId:52,cpage:this.selectionPage(),psize:100,compact:true,uid:student?.userId}).pipe(finalize(()=>{this.selectionLoading.set(false);this.loading.set(false);})).subscribe({next:({result,page})=>{
|
|
|
accept(result,page?.itemCount);
|
|
|
+ if (!more) this.prepareWordAudio(this.visibleCourseWords()[0]);
|
|
|
if (rebaseFromPage !== undefined) this.courseSelectionNeedsRebase = false;
|
|
|
const merged=new Map(this.courseSelectionCache().map(word=>[word.id,word]));
|
|
|
for(const word of this.words()) merged.set(word.id,word);
|
|
|
@@ -1663,6 +1700,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
return;
|
|
|
}
|
|
|
this.words.set(incoming);
|
|
|
+ this.prepareWordAudio(incoming[0]);
|
|
|
for (const word of incoming) this.newWordSelectionCache.set(word.id, word);
|
|
|
const counts = {pending: pendingCount, completed: completedCount};
|
|
|
this.newWordCounts.set(counts);
|
|
|
@@ -1740,7 +1778,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
const words = this.isStudyListPage ? source : this.shuffle(source);
|
|
|
this.words.set(words);
|
|
|
- this.prepareWordAudio(words[0]);
|
|
|
+ if (!this.isListeningPage && !this.isSpellingPage) this.prepareWordAudio(words[0]);
|
|
|
if (this.isStudyListPage) {
|
|
|
this.studyStep.set(1);
|
|
|
this.repetitions.set({});
|
|
|
@@ -1752,11 +1790,11 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
const round = [...this.readAllLearningWords(), ...words];
|
|
|
this.listeningPool.load(round.map(word => word.id)).subscribe({ error: () => undefined });
|
|
|
} else if (this.isListeningPage) {
|
|
|
+ void this.play();
|
|
|
this.prepareChoiceOptions();
|
|
|
- setTimeout(() => this.play(), 120);
|
|
|
} else if (this.isSpellingPage) {
|
|
|
this.prepareSpelling();
|
|
|
- setTimeout(() => this.play(), 120);
|
|
|
+ void this.play();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1764,8 +1802,10 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
if (this.studyStep() < 3) {
|
|
|
const next = (this.studyStep() + 1) as 2 | 3;
|
|
|
this.selectionRevealed.set(new Set());
|
|
|
+ this.pointReadAudio.set(null);
|
|
|
this.studyStep.set(next);
|
|
|
this.words.set(shuffledStudy(this.words()));
|
|
|
+ this.prepareWordAudio(this.words()[0]);
|
|
|
this.repetitions.set({});
|
|
|
this.studyMeaningRevealed.set(new Set());
|
|
|
this.platform.toast(next === 2 ? '进入乱序学习' : '进入中文挑战', 'success');
|
|
|
@@ -2441,9 +2481,7 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
private clearLearningStorage(): void { this.draft().clear(); }
|
|
|
|
|
|
private resetPageState(): void {
|
|
|
- this.audioRequest++;
|
|
|
- this.platform.stopAudio();
|
|
|
- this.audioPreparing.set(false);
|
|
|
+ this.stopWordPlayback();
|
|
|
this.outsideChoicePool = null;
|
|
|
this.choicesLoading.set(false);
|
|
|
this.choiceOptionsVersion++;
|
|
|
@@ -2454,7 +2492,6 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
this.words.set([]);
|
|
|
this.historyPreviewWords = [];
|
|
|
this.historyWordRevealed.set(new Set());
|
|
|
- this.historyAudioWordId.set('');
|
|
|
this.historySources.set([]);
|
|
|
this.wordPreviewPage.set(1);
|
|
|
this.wordPreviewTotal.set(0);
|
|
|
@@ -2513,9 +2550,10 @@ export class LearningPageComponent implements OnInit, OnDestroy {
|
|
|
wordId,
|
|
|
nodeId: String(item['NodeID'] ?? detail['NodeID'] ?? item['nodeId'] ?? ''),
|
|
|
word: String(item['Title'] ?? detail['Title'] ?? item['word'] ?? detail['word'] ?? item['Word'] ?? ''),
|
|
|
- meaning: String(item['sy'] ?? detail['sy'] ?? item['jssy'] ?? detail['jssy'] ?? item['Translate'] ?? detail['Translate'] ?? item['Meaning'] ?? ''),
|
|
|
+ meaning: [item['sy'], detail['sy'], item['jssy'], detail['jssy'], item['Translate'], detail['Translate'], item['Meaning'], item['meaning']]
|
|
|
+ .map(value => String(value ?? '').trim()).find(Boolean) || '',
|
|
|
phonetic: [item['yb'], detail['yb'], item['Phonetic'], detail['Phonetic'], item['phonetic'], detail['phonetic']].map(value => String(value ?? '').trim()).find(Boolean) || '',
|
|
|
- audio: [item['yp'], detail['yp'], item['dy'], detail['dy']].map(value => String(value ?? '').trim()).find(Boolean) || '',
|
|
|
+ audio: [typeof item['audio'] === 'string' ? item['audio'] : '', item['yp'], detail['yp'], item['dy'], detail['dy']].map(value => String(value ?? '').trim()).find(Boolean) || '',
|
|
|
};
|
|
|
}
|
|
|
|