41 lines
1.2 KiB
Dart
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).
|
|
}
|
|
}
|
|
}
|