fix(tts): 修复Android包可见性导致TTS未绑定问题,优化语音时序与引擎探测
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'services/storage_service.dart';
|
||||
import 'services/tts_service.dart';
|
||||
import 'screens/home_screen.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final storageService = await StorageService.init();
|
||||
TtsService().init();
|
||||
runApp(AeroTrackApp(storageService: storageService));
|
||||
}
|
||||
|
||||
|
||||
@@ -148,6 +148,19 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
void _speakStep(SessionStep step) {
|
||||
if (!_settings.enableVoicePrompt) return;
|
||||
if (step.type == StepType.inhale) {
|
||||
final medName = step.canister?.name.split(" ")[0] ?? '药物';
|
||||
TtsService().speak('请吸入$medName第${step.puffIndex}喷,口鼻密闭,慢而深地吸气。');
|
||||
} else if (step.type == StepType.intraPuffWait) {
|
||||
TtsService().speak('第${step.puffIndex}喷吸入完成,请等待三十秒后吸入下一喷。');
|
||||
} else if (step.type == StepType.crossDrugWait) {
|
||||
final minutes = step.durationSeconds ~/ 60;
|
||||
TtsService().speak('两药间隔开始,请等待$minutes分钟,待气道充分扩张。');
|
||||
}
|
||||
}
|
||||
|
||||
void _startStep(int index) {
|
||||
if (index >= _steps.length) {
|
||||
_finishSession();
|
||||
@@ -163,6 +176,8 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
_totalStageSeconds = step.durationSeconds;
|
||||
});
|
||||
|
||||
_speakStep(step);
|
||||
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (_remainingSeconds > 1) {
|
||||
setState(() {
|
||||
@@ -178,17 +193,6 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
void _onStepCompleted() {
|
||||
_triggerFeedback();
|
||||
if (_currentStepIndex + 1 < _steps.length) {
|
||||
final nextStep = _steps[_currentStepIndex + 1];
|
||||
if (_settings.enableVoicePrompt) {
|
||||
if (nextStep.type == StepType.crossDrugWait) {
|
||||
TtsService().speak('两药间隔开始,请等待五分钟');
|
||||
} else if (nextStep.type == StepType.inhale) {
|
||||
final medName = nextStep.canister?.name.split(" ")[0] ?? '药物';
|
||||
TtsService().speak('请吸入$medName第${nextStep.puffIndex}喷');
|
||||
} else if (nextStep.type == StepType.intraPuffWait) {
|
||||
TtsService().speak('请等待三十秒后喷第二喷');
|
||||
}
|
||||
}
|
||||
_startStep(_currentStepIndex + 1);
|
||||
} else {
|
||||
_finishSession();
|
||||
|
||||
@@ -203,7 +203,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
const Divider(height: 1),
|
||||
SwitchListTile(
|
||||
title: const Text('真人语音播报', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
|
||||
subtitle: const Text('做完时自动播报“本次雾化吸入已全部完成,请及时用清水漱口并洗脸”', style: TextStyle(fontSize: 12)),
|
||||
subtitle: const Text('各阶段步骤语音提醒与完成时漱口洗脸指引(需开启媒体音量)', style: TextStyle(fontSize: 12)),
|
||||
value: _settings.enableVoicePrompt,
|
||||
activeTrackColor: const Color(0xFF2563EB),
|
||||
onChanged: (val) {
|
||||
@@ -213,19 +213,36 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 12),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
|
||||
},
|
||||
style: OutlinedButton.styleFrom(
|
||||
visualDensity: VisualDensity.compact,
|
||||
side: const BorderSide(color: Color(0xFFBFDBFE)),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
onPressed: () async {
|
||||
final ok = await TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(ok ? '已发送语音播放请求,请确认手机媒体音量已开启' : '语音播放请求已发送,如未听到请检查系统TTS设置与媒体音量'),
|
||||
duration: const Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
style: OutlinedButton.styleFrom(
|
||||
visualDensity: VisualDensity.compact,
|
||||
side: const BorderSide(color: Color(0xFFBFDBFE)),
|
||||
),
|
||||
icon: const Icon(Icons.volume_up, size: 16, color: Color(0xFF2563EB)),
|
||||
label: const Text('🔊 点击试听语音播报效果', style: TextStyle(fontSize: 12, color: Color(0xFF2563EB))),
|
||||
),
|
||||
icon: const Icon(Icons.volume_up, size: 16, color: Color(0xFF2563EB)),
|
||||
label: const Text('🔊 点击试听语音播报效果', style: TextStyle(fontSize: 12, color: Color(0xFF2563EB))),
|
||||
),
|
||||
if (TtsService().selectedEngine != null) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'当前语音引擎: ${TtsService().selectedEngine}',
|
||||
style: TextStyle(fontSize: 11, color: Colors.grey.shade500),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -8,29 +8,77 @@ class TtsService {
|
||||
|
||||
final FlutterTts _flutterTts = FlutterTts();
|
||||
bool _initialized = false;
|
||||
String? _selectedEngine;
|
||||
List<String> _availableEngines = [];
|
||||
|
||||
List<String> get availableEngines => _availableEngines;
|
||||
String? get selectedEngine => _selectedEngine;
|
||||
|
||||
Future<void> init() async {
|
||||
if (_initialized) return;
|
||||
try {
|
||||
if (!kIsWeb) {
|
||||
// Query engines on Android
|
||||
try {
|
||||
final dynamic engines = await _flutterTts.getEngines;
|
||||
if (engines is List) {
|
||||
_availableEngines = engines.map((e) => e.toString()).toList();
|
||||
debugPrint('Available TTS engines: $_availableEngines');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Error getting TTS engines: $e');
|
||||
}
|
||||
|
||||
try {
|
||||
final dynamic defaultEngine = await _flutterTts.getDefaultEngine;
|
||||
debugPrint('Default TTS engine: $defaultEngine');
|
||||
if (defaultEngine != null && defaultEngine.toString().isNotEmpty) {
|
||||
_selectedEngine = defaultEngine.toString();
|
||||
await _flutterTts.setEngine(_selectedEngine!);
|
||||
} else if (_availableEngines.isNotEmpty) {
|
||||
_selectedEngine = _availableEngines.first;
|
||||
await _flutterTts.setEngine(_selectedEngine!);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Error setting TTS engine: $e');
|
||||
}
|
||||
|
||||
try {
|
||||
await _flutterTts.setIosAudioCategory(
|
||||
IosTextToSpeechAudioCategory.playback,
|
||||
[
|
||||
IosTextToSpeechAudioCategoryOptions.duckOthers,
|
||||
IosTextToSpeechAudioCategoryOptions.defaultToSpeaker,
|
||||
],
|
||||
);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
await _flutterTts.setLanguage("zh-CN");
|
||||
await _flutterTts.setSpeechRate(0.5); // 适中语速
|
||||
await _flutterTts.setVolume(1.0); // 满音量
|
||||
await _flutterTts.setPitch(1.0);
|
||||
await _flutterTts.awaitSpeakCompletion(false);
|
||||
|
||||
_initialized = true;
|
||||
debugPrint('TTS initialized successfully (engine: $_selectedEngine)');
|
||||
} catch (e) {
|
||||
debugPrint('TTS init error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> speak(String text) async {
|
||||
Future<bool> speak(String text) async {
|
||||
try {
|
||||
if (!_initialized) {
|
||||
await init();
|
||||
}
|
||||
await _flutterTts.stop();
|
||||
await _flutterTts.speak(text);
|
||||
final dynamic result = await _flutterTts.speak(text);
|
||||
debugPrint('TTS speak "$text" -> $result');
|
||||
return result == 1;
|
||||
} catch (e) {
|
||||
debugPrint('TTS speak error: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user