feat: 增加AI配置文件与TTS自动朗读,修复二级页面返回按钮与音频播放

This commit is contained in:
shenlei
2026-09-15 19:11:39 +09:00
parent 37c86f7ecb
commit 4341726162
18 changed files with 1322 additions and 397 deletions
+44 -6
View File
@@ -17,15 +17,53 @@ class VoiceService {
final AudioRecorder _recorder = AudioRecorder();
final AudioPlayer _player = AudioPlayer();
bool _speechReady = false;
bool _ttsInitialized = false;
Future<void> speak(String text, {bool slow = false}) async {
await _tts.stop();
await _tts.setLanguage('en-US');
await _tts.setSpeechRate(slow ? 0.35 : 0.48);
await _tts.speak(text);
Future<void> _initTts() async {
if (_ttsInitialized) return;
try {
if (Platform.isIOS) {
await _tts.setIosAudioCategory(
IosTextToSpeechAudioCategory.playback,
[
IosTextToSpeechAudioCategoryOptions.allowBluetooth,
IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
IosTextToSpeechAudioCategoryOptions.mixWithOthers,
],
);
}
await _tts.setVolume(1.0);
await _tts.setPitch(1.0);
_ttsInitialized = true;
} catch (_) {}
}
Future<void> stopSpeaking() => _tts.stop();
Future<void> speak(String text, {bool slow = false}) async {
try {
await _initTts();
await _tts.stop();
try {
final isAvailable = await _tts.isLanguageAvailable('en-US');
if (isAvailable == true) {
await _tts.setLanguage('en-US');
} else {
await _tts.setLanguage('en');
}
} catch (_) {
try {
await _tts.setLanguage('en-US');
} catch (_) {}
}
await _tts.setSpeechRate(slow ? 0.35 : 0.48);
await _tts.speak(text);
} catch (_) {}
}
Future<void> stopSpeaking() async {
try {
await _tts.stop();
} catch (_) {}
}
Future<bool> startRecording() async {
if (!await _recorder.hasPermission()) return false;