fix(tts): 修复Android包可见性导致TTS未绑定问题,优化语音时序与引擎探测

This commit is contained in:
shen
2026-10-27 04:11:57 -07:00
parent cd84d308cb
commit 473fbb93ad
5 changed files with 100 additions and 26 deletions
+15 -11
View File
@@ -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();
+30 -13
View File
@@ -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),
),
],
],
),
),
],