import 'package:em2rp/services/cloud_text_to_speech_service.dart'; import 'package:em2rp/utils/debug_log.dart'; /// Service de synthèse vocale utilisant exclusivement Google Cloud TTS /// Garantit une qualité et une compatibilité maximales sur tous les navigateurs class SmartTextToSpeechService { static bool _initialized = false; /// Initialiser le service static Future initialize() async { if (_initialized) return; try { DebugLog.info('[SmartTTS] Initializing Cloud TTS only...'); // Pré-charger les phrases courantes pour Cloud TTS Future.delayed(const Duration(milliseconds: 500), () { CloudTextToSpeechService.preloadCommonPhrases(); }); _initialized = true; DebugLog.info('[SmartTTS] ✓ Initialized (Cloud TTS only)'); } catch (e) { DebugLog.error('[SmartTTS] Initialization error', e); _initialized = true; // Continuer quand même } } /// Lire un texte à haute voix avec Google Cloud TTS static Future speak(String text) async { if (!_initialized) { await initialize(); } try { DebugLog.info('[SmartTTS] → Using Cloud TTS'); await CloudTextToSpeechService.speak(text); DebugLog.info('[SmartTTS] ✓ Cloud TTS succeeded'); } catch (e) { DebugLog.error('[SmartTTS] ✗ Cloud TTS failed', e); rethrow; } } /// Arrêter toute lecture en cours static Future stop() async { try { CloudTextToSpeechService.stopAll(); } catch (e) { DebugLog.error('[SmartTTS] Error stopping', e); } } /// Vérifier si une lecture est en cours static Future isSpeaking() async { // Cloud TTS n'a pas de méthode native pour vérifier le statut // Retourner false par défaut (peut être amélioré si nécessaire) return false; } /// Obtenir le statut actuel static Map getStatus() { return { 'initialized': _initialized, 'currentStrategy': 'Cloud TTS (exclusive)', }; } /// Nettoyer les ressources static Future dispose() async { try { CloudTextToSpeechService.clearCache(); } catch (e) { DebugLog.error('[SmartTTS] Error disposing', e); } } }