44 lines
1.0 KiB
Dart
44 lines
1.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_tts/flutter_tts.dart';
|
|
|
|
import 'spelling_tts_backend.dart';
|
|
|
|
SpellingTtsBackend createSpellingTtsBackend() => _FlutterTtsBackend();
|
|
|
|
class _FlutterTtsBackend implements SpellingTtsBackend {
|
|
final FlutterTts _tts = FlutterTts();
|
|
bool _configured = false;
|
|
|
|
Future<void> _ensureConfigured() async {
|
|
if (_configured) {
|
|
return;
|
|
}
|
|
await _tts.setLanguage('en-US');
|
|
await _tts.setSpeechRate(0.42);
|
|
await _tts.setPitch(1.0);
|
|
await _tts.awaitSpeakCompletion(true);
|
|
_configured = true;
|
|
}
|
|
|
|
@override
|
|
Future<void> speak(String text, {String? title}) async {
|
|
final String trimmed = text.trim();
|
|
if (trimmed.isEmpty) {
|
|
return;
|
|
}
|
|
await _ensureConfigured();
|
|
await _tts.stop();
|
|
if (title != null && title.isNotEmpty) {
|
|
debugPrint('Spelling TTS utterance title: $title');
|
|
}
|
|
await _tts.speak(trimmed);
|
|
}
|
|
|
|
@override
|
|
Future<void> stop() async {
|
|
try {
|
|
await _tts.stop();
|
|
} catch (_) {}
|
|
}
|
|
}
|