cyberhybridhub/lib/services/spelling_text_to_speech.dart
nathanAndrinoid 4459852f83
Some checks failed
Admin portal tests / test (push) Has been cancelled
spelling
2026-06-05 03:22:35 +00:00

41 lines
1.2 KiB
Dart

import 'package:flutter/foundation.dart';
import 'spelling_tts/spelling_tts_backend.dart';
import 'spelling_tts/spelling_tts_flutter.dart'
if (dart.library.js_interop) 'spelling_tts/spelling_tts_web.dart';
/// English system text-to-speech for spelling challenges.
///
/// On web, uses the browser [speechSynthesis] API directly (avoids
/// [flutter_tts] method-channel registration issues). On IO platforms, uses
/// [flutter_tts].
class SpellingTextToSpeech {
SpellingTextToSpeech({SpellingTtsBackend? backend})
: _backend = backend ?? createSpellingTtsBackend();
final SpellingTtsBackend _backend;
/// Speaks [text] using the platform TTS engine (clears any in-progress utterance).
///
/// [title] references the question id for the utterance (logged for diagnostics).
Future<void> speak(String text, {String? title}) async {
final String trimmed = text.trim();
if (trimmed.isEmpty) {
return;
}
try {
await _backend.speak(trimmed, title: title);
} catch (e, st) {
debugPrint('Spelling TTS failed: $e\n$st');
}
}
Future<void> stop() async {
try {
await _backend.stop();
} catch (_) {
// Ignore shutdown errors (e.g. in widget tests without engine).
}
}
}