Jkdsjksj
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from "expo-av";
|
||||
import * as Speech from "expo-speech";
|
||||
import { router, useFocusEffect } from "expo-router";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import Svg, { Path } from "react-native-svg";
|
||||
@@ -63,7 +62,9 @@ export default function RecorderScreen() {
|
||||
const [transcriptionText, setTranscriptionText] = useState("");
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const [isHolding, setIsHolding] = useState(false);
|
||||
const [isPlaying, setIsPlaying] = useState(false);
|
||||
const recordingRef = useRef<Audio.Recording | null>(null);
|
||||
const soundRef = useRef<Audio.Sound | null>(null);
|
||||
|
||||
const refreshSettings = useCallback(() => {
|
||||
let isMounted = true;
|
||||
@@ -123,15 +124,29 @@ export default function RecorderScreen() {
|
||||
};
|
||||
}, [recording]);
|
||||
|
||||
async function speak(text: string) {
|
||||
if (!text || !text.trim()) {
|
||||
console.log("[TTS] Skipping empty text");
|
||||
return;
|
||||
}
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
void unloadSound();
|
||||
};
|
||||
}, []);
|
||||
|
||||
console.log("[TTS] ===== START speak =====");
|
||||
Speech.stop();
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
async function unloadSound() {
|
||||
if (soundRef.current) {
|
||||
try {
|
||||
await soundRef.current.stopAsync();
|
||||
await soundRef.current.unloadAsync();
|
||||
} catch (err) {
|
||||
console.log("[TTS] Error unloading sound:", err);
|
||||
}
|
||||
soundRef.current = null;
|
||||
}
|
||||
setIsPlaying(false);
|
||||
}
|
||||
|
||||
async function speakWithAudio(audioUrl: string, backendBase: string) {
|
||||
if (!audioUrl) return false;
|
||||
|
||||
await unloadSound();
|
||||
|
||||
try {
|
||||
await Audio.setAudioModeAsync({
|
||||
@@ -142,38 +157,103 @@ export default function RecorderScreen() {
|
||||
shouldDuckAndroid: true,
|
||||
staysActiveInBackground: false,
|
||||
});
|
||||
console.log("[TTS] Audio mode reset OK");
|
||||
} catch (err) {
|
||||
console.log("[TTS] Audio mode error:", err);
|
||||
}
|
||||
|
||||
const lang = locale === "ca" ? "ca-ES" : "en-US";
|
||||
await new Promise((r) => setTimeout(r, 800));
|
||||
|
||||
console.log("[TTS] Calling Speech.speak. Text length:", text.length, "Lang:", lang);
|
||||
try {
|
||||
Speech.speak(text, {
|
||||
language: lang,
|
||||
onDone: () => console.log("[TTS] ✅ onDone fired"),
|
||||
onError: (error) => console.log("[TTS] ❌ onError:", error),
|
||||
});
|
||||
console.log("[TTS] Speech.speak() call returned OK");
|
||||
const fullUrl = audioUrl.startsWith("http")
|
||||
? audioUrl
|
||||
: `${backendBase.replace(/\/+$/, "")}/${audioUrl.replace(/^\/+/, "")}`;
|
||||
|
||||
console.log("[TTS] Loading audio from:", fullUrl);
|
||||
setIsPlaying(true);
|
||||
setStatusMessage(strings.playing);
|
||||
|
||||
const { sound } = await Audio.Sound.createAsync(
|
||||
{ uri: fullUrl },
|
||||
{ shouldPlay: true, volume: 1.0 },
|
||||
(status) => {
|
||||
if (status.isLoaded && status.didJustFinish) {
|
||||
console.log("[TTS] Audio playback finished");
|
||||
void unloadSound();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
soundRef.current = sound;
|
||||
const status = await sound.getStatusAsync();
|
||||
const durationMs = status.isLoaded ? (status.durationMillis ?? 0) : 0;
|
||||
console.log("[TTS] Playing audio, duration:", durationMs, "ms");
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log("[TTS] Speech.speak() threw:", err);
|
||||
console.log("[TTS] Audio playback error:", err);
|
||||
setIsPlaying(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function speakSequentially(texts: string[]) {
|
||||
if (texts.length === 0) return;
|
||||
|
||||
const trimmedUrl = backendUrl.trim().replace(/\/+$/, "");
|
||||
for (let i = 0; i < texts.length; i++) {
|
||||
await speak(texts[i]);
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
const text = texts[i];
|
||||
if (!text || !text.trim()) continue;
|
||||
|
||||
try {
|
||||
setStatusMessage(strings.playing);
|
||||
console.log("[TTS] Generating TTS audio for text:", text.substring(0, 50));
|
||||
|
||||
const localeLang = locale === "ca" ? "ca" : "en";
|
||||
const ttsParams = new URLSearchParams({
|
||||
text: text.trim(),
|
||||
language: localeLang,
|
||||
});
|
||||
|
||||
if (authToken.trim()) {
|
||||
ttsParams.append("token", authToken.trim());
|
||||
}
|
||||
|
||||
const ttsUrl = `${trimmedUrl}/tts?${ttsParams.toString()}`;
|
||||
const ttsResponse = await fetch(ttsUrl, { method: "POST" });
|
||||
|
||||
if (!ttsResponse.ok) {
|
||||
const errText = await ttsResponse.text();
|
||||
console.log("[TTS] TTS endpoint error:", ttsResponse.status, errText);
|
||||
continue;
|
||||
}
|
||||
|
||||
const ttsData = await ttsResponse.json();
|
||||
|
||||
if (!ttsData.audioUrl) {
|
||||
console.log("[TTS] No audioUrl in response:", ttsData);
|
||||
continue;
|
||||
}
|
||||
|
||||
const played = await speakWithAudio(ttsData.audioUrl, trimmedUrl);
|
||||
if (!played) {
|
||||
setStatusMessage(strings.uploadFailed);
|
||||
}
|
||||
|
||||
if (i < texts.length - 1) {
|
||||
await new Promise((r) => setTimeout(r, 800));
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("[TTS] speakSequentially error:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function speak(text: string) {
|
||||
const texts = [text].filter(Boolean);
|
||||
await speakSequentially(texts);
|
||||
}
|
||||
|
||||
async function startRecording() {
|
||||
try {
|
||||
Speech.stop();
|
||||
await unloadSound();
|
||||
setTranscriptionText("");
|
||||
setResponsePreview("");
|
||||
setLlmResponseText("");
|
||||
@@ -367,7 +447,7 @@ export default function RecorderScreen() {
|
||||
try {
|
||||
setIsUploading(true);
|
||||
setStatusMessage(strings.uploadingRecording);
|
||||
Speech.stop();
|
||||
await unloadSound();
|
||||
setTranscriptionText("");
|
||||
setResponsePreview("");
|
||||
setLlmResponseText("");
|
||||
|
||||
Reference in New Issue
Block a user