fix(tts): 修复Android包可见性导致TTS未绑定问题,优化语音时序与引擎探测
This commit is contained in:
@@ -42,6 +42,9 @@
|
|||||||
|
|
||||||
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
|
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
|
||||||
<queries>
|
<queries>
|
||||||
|
<intent>
|
||||||
|
<action android:name="android.intent.action.TTS_SERVICE" />
|
||||||
|
</intent>
|
||||||
<intent>
|
<intent>
|
||||||
<action android:name="android.intent.action.PROCESS_TEXT"/>
|
<action android:name="android.intent.action.PROCESS_TEXT"/>
|
||||||
<data android:mimeType="text/plain"/>
|
<data android:mimeType="text/plain"/>
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'services/storage_service.dart';
|
import 'services/storage_service.dart';
|
||||||
|
import 'services/tts_service.dart';
|
||||||
import 'screens/home_screen.dart';
|
import 'screens/home_screen.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
final storageService = await StorageService.init();
|
final storageService = await StorageService.init();
|
||||||
|
TtsService().init();
|
||||||
runApp(AeroTrackApp(storageService: storageService));
|
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) {
|
void _startStep(int index) {
|
||||||
if (index >= _steps.length) {
|
if (index >= _steps.length) {
|
||||||
_finishSession();
|
_finishSession();
|
||||||
@@ -163,6 +176,8 @@ class _SessionScreenState extends State<SessionScreen> {
|
|||||||
_totalStageSeconds = step.durationSeconds;
|
_totalStageSeconds = step.durationSeconds;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_speakStep(step);
|
||||||
|
|
||||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||||
if (_remainingSeconds > 1) {
|
if (_remainingSeconds > 1) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -178,17 +193,6 @@ class _SessionScreenState extends State<SessionScreen> {
|
|||||||
void _onStepCompleted() {
|
void _onStepCompleted() {
|
||||||
_triggerFeedback();
|
_triggerFeedback();
|
||||||
if (_currentStepIndex + 1 < _steps.length) {
|
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);
|
_startStep(_currentStepIndex + 1);
|
||||||
} else {
|
} else {
|
||||||
_finishSession();
|
_finishSession();
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
SwitchListTile(
|
SwitchListTile(
|
||||||
title: const Text('真人语音播报', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
|
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,
|
value: _settings.enableVoicePrompt,
|
||||||
activeTrackColor: const Color(0xFF2563EB),
|
activeTrackColor: const Color(0xFF2563EB),
|
||||||
onChanged: (val) {
|
onChanged: (val) {
|
||||||
@@ -213,19 +213,36 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
),
|
),
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 12),
|
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 12),
|
||||||
child: Align(
|
child: Column(
|
||||||
alignment: Alignment.centerLeft,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
child: OutlinedButton.icon(
|
children: [
|
||||||
onPressed: () {
|
OutlinedButton.icon(
|
||||||
TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
|
onPressed: () async {
|
||||||
},
|
final ok = await TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
|
||||||
style: OutlinedButton.styleFrom(
|
if (context.mounted) {
|
||||||
visualDensity: VisualDensity.compact,
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
side: const BorderSide(color: Color(0xFFBFDBFE)),
|
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)),
|
if (TtsService().selectedEngine != null) ...[
|
||||||
label: const Text('🔊 点击试听语音播报效果', style: TextStyle(fontSize: 12, color: Color(0xFF2563EB))),
|
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();
|
final FlutterTts _flutterTts = FlutterTts();
|
||||||
bool _initialized = false;
|
bool _initialized = false;
|
||||||
|
String? _selectedEngine;
|
||||||
|
List<String> _availableEngines = [];
|
||||||
|
|
||||||
|
List<String> get availableEngines => _availableEngines;
|
||||||
|
String? get selectedEngine => _selectedEngine;
|
||||||
|
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
if (_initialized) return;
|
if (_initialized) return;
|
||||||
try {
|
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.setLanguage("zh-CN");
|
||||||
await _flutterTts.setSpeechRate(0.5); // 适中语速
|
await _flutterTts.setSpeechRate(0.5); // 适中语速
|
||||||
await _flutterTts.setVolume(1.0); // 满音量
|
await _flutterTts.setVolume(1.0); // 满音量
|
||||||
await _flutterTts.setPitch(1.0);
|
await _flutterTts.setPitch(1.0);
|
||||||
|
await _flutterTts.awaitSpeakCompletion(false);
|
||||||
|
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
|
debugPrint('TTS initialized successfully (engine: $_selectedEngine)');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint('TTS init error: $e');
|
debugPrint('TTS init error: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> speak(String text) async {
|
Future<bool> speak(String text) async {
|
||||||
try {
|
try {
|
||||||
if (!_initialized) {
|
if (!_initialized) {
|
||||||
await init();
|
await init();
|
||||||
}
|
}
|
||||||
await _flutterTts.stop();
|
await _flutterTts.stop();
|
||||||
await _flutterTts.speak(text);
|
final dynamic result = await _flutterTts.speak(text);
|
||||||
|
debugPrint('TTS speak "$text" -> $result');
|
||||||
|
return result == 1;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint('TTS speak error: $e');
|
debugPrint('TTS speak error: $e');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user