Übungssätze mit Azure-Stimme abspielbar machen
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { RotateCcw, Send } from 'lucide-react';
|
||||
import { RotateCcw, Send, Volume2 } from 'lucide-react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { fetchNextLesson } from '../api/lessons';
|
||||
import { fetchExerciseAudio, fetchNextLesson } from '../api/lessons';
|
||||
import { submitAttempt } from '../api/practice';
|
||||
import type { AttemptResponse, Exercise, Lesson } from '../api/types';
|
||||
import { AudioRecorder } from '../components/AudioRecorder';
|
||||
@@ -21,9 +21,14 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [loadingNext, setLoadingNext] = useState(false);
|
||||
const [recording, setRecording] = useState(false);
|
||||
const [audioLoading, setAudioLoading] = useState(false);
|
||||
const [audioError, setAudioError] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [notice, setNotice] = useState('');
|
||||
const submittingRef = useRef(false);
|
||||
const referenceAudioRef = useRef<{ exerciseId: string; url: string; player: HTMLAudioElement } | null>(null);
|
||||
const audioRequestRef = useRef<AbortController | null>(null);
|
||||
const audioGenerationRef = useRef(0);
|
||||
const recentExerciseIds = useRef<string[]>(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -40,11 +45,54 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
return () => URL.revokeObjectURL(url);
|
||||
}, [audio]);
|
||||
|
||||
useEffect(() => () => {
|
||||
audioGenerationRef.current += 1;
|
||||
audioRequestRef.current?.abort();
|
||||
audioRequestRef.current = null;
|
||||
referenceAudioRef.current?.player.pause();
|
||||
if (referenceAudioRef.current) URL.revokeObjectURL(referenceAudioRef.current.url);
|
||||
referenceAudioRef.current = null;
|
||||
}, [exercise?.id]);
|
||||
|
||||
async function playReference() {
|
||||
if (!exercise || audioLoading || audioRequestRef.current || recording) return;
|
||||
setAudioError('');
|
||||
const exerciseId = exercise.id;
|
||||
const generation = audioGenerationRef.current;
|
||||
let controller: AbortController | null = null;
|
||||
try {
|
||||
if (!referenceAudioRef.current || referenceAudioRef.current.exerciseId !== exerciseId) {
|
||||
setAudioLoading(true);
|
||||
controller = new AbortController();
|
||||
audioRequestRef.current = controller;
|
||||
const blob = await fetchExerciseAudio(exerciseId, controller.signal);
|
||||
if (controller.signal.aborted || audioGenerationRef.current !== generation) return;
|
||||
const url = URL.createObjectURL(blob);
|
||||
referenceAudioRef.current = { exerciseId, url, player: new Audio(url) };
|
||||
}
|
||||
const player = referenceAudioRef.current.player;
|
||||
player.currentTime = 0;
|
||||
await player.play();
|
||||
} catch {
|
||||
if (audioGenerationRef.current !== generation || controller?.signal.aborted) return;
|
||||
setAudioError('Satz konnte nicht abgespielt werden. Bitte erneut versuchen.');
|
||||
} finally {
|
||||
if (audioGenerationRef.current === generation) setAudioLoading(false);
|
||||
if (audioRequestRef.current === controller) audioRequestRef.current = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadNext(fresh = false) {
|
||||
if (loadingNext || recording || submittingRef.current) return;
|
||||
audioGenerationRef.current += 1;
|
||||
audioRequestRef.current?.abort();
|
||||
audioRequestRef.current = null;
|
||||
referenceAudioRef.current?.player.pause();
|
||||
setAudioLoading(false);
|
||||
setLoadingNext(true);
|
||||
setError('');
|
||||
setNotice('');
|
||||
setAudioError('');
|
||||
setAudio(null);
|
||||
setResult(null);
|
||||
try {
|
||||
@@ -107,11 +155,24 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
<section className="panel practice-panel">
|
||||
<p className="muted">{exercise.prompt_text}</p>
|
||||
<PronunciationText targetText={exercise.target_text} words={result?.attempt.word_scores} />
|
||||
<button className="ghost-button" type="button" disabled={loadingNext || audioLoading || recording} onClick={() => void playReference()}>
|
||||
<Volume2 size={18} /> {audioLoading ? 'Satz wird geladen...' : 'Satz anhören'}
|
||||
</button>
|
||||
{audioError && <p className="form-error">{audioError}</p>}
|
||||
<div className="action-row">
|
||||
<AudioRecorder
|
||||
key={exercise.id}
|
||||
onRecorded={setAudio}
|
||||
onRecordingStart={() => { setAudio(null); setResult(null); setError(''); }}
|
||||
onRecordingStart={() => {
|
||||
audioGenerationRef.current += 1;
|
||||
audioRequestRef.current?.abort();
|
||||
audioRequestRef.current = null;
|
||||
referenceAudioRef.current?.player.pause();
|
||||
setAudioLoading(false);
|
||||
setAudio(null);
|
||||
setResult(null);
|
||||
setError('');
|
||||
}}
|
||||
onRecordingChange={setRecording}
|
||||
disabled={loading || loadingNext}
|
||||
buttonLabel={loading ? 'Bewertung läuft...' : audio || result ? 'Erneut aufnehmen' : 'Aufnehmen'}
|
||||
|
||||
Reference in New Issue
Block a user