feat(audio): 引入audioplayers原生提示音效,修复做完提示音缺失与局域网配置溢出
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'services/storage_service.dart';
|
||||
import 'services/tts_service.dart';
|
||||
import 'services/audio_service.dart';
|
||||
import 'screens/home_screen.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final storageService = await StorageService.init();
|
||||
TtsService().init();
|
||||
AudioService().init();
|
||||
runApp(AeroTrackApp(storageService: storageService));
|
||||
}
|
||||
|
||||
|
||||
@@ -218,15 +218,20 @@ class _LanSyncScreenState extends State<LanSyncScreen> {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
// 快捷预设按钮
|
||||
Row(
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
ActionChip(
|
||||
label: const Text('填入 192.168.21.200:8090', style: TextStyle(fontSize: 11)),
|
||||
visualDensity: VisualDensity.compact,
|
||||
avatar: const Icon(Icons.flash_on, size: 14, color: Color(0xFF2563EB)),
|
||||
label: const Text('192.168.21.200:8090', style: TextStyle(fontSize: 11)),
|
||||
onPressed: () => setState(() => _urlController.text = 'http://192.168.21.200:8090'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ActionChip(
|
||||
label: const Text('填入 localhost:8090', style: TextStyle(fontSize: 11)),
|
||||
visualDensity: VisualDensity.compact,
|
||||
avatar: const Icon(Icons.computer, size: 14, color: Color(0xFF475569)),
|
||||
label: const Text('localhost:8090', style: TextStyle(fontSize: 11)),
|
||||
onPressed: () => setState(() => _urlController.text = 'http://localhost:8090'),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -8,6 +8,7 @@ import '../models/dose_record.dart';
|
||||
import '../services/storage_service.dart';
|
||||
import '../services/tts_service.dart';
|
||||
import '../services/sync_service.dart';
|
||||
import '../services/audio_service.dart';
|
||||
|
||||
enum StepType {
|
||||
inhale, // 正在吸入某药某喷
|
||||
@@ -139,12 +140,16 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _triggerFeedback() {
|
||||
void _triggerFeedback({bool isFinished = false}) {
|
||||
if (_settings.enableVibration) {
|
||||
HapticFeedback.heavyImpact();
|
||||
}
|
||||
if (_settings.enableSound) {
|
||||
SystemSound.play(SystemSoundType.alert);
|
||||
if (isFinished) {
|
||||
AudioService().playSessionComplete();
|
||||
} else {
|
||||
AudioService().playStepComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,9 +207,13 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
void _finishSession() {
|
||||
_timer?.cancel();
|
||||
_sessionEndTime = DateTime.now();
|
||||
_triggerFeedback();
|
||||
_triggerFeedback(isFinished: true);
|
||||
if (_settings.enableVoicePrompt) {
|
||||
TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
|
||||
Future.delayed(const Duration(milliseconds: 1000), () {
|
||||
if (mounted) {
|
||||
TtsService().speak('本次雾化吸入已全部完成,请及时用清水漱口并洗脸!');
|
||||
}
|
||||
});
|
||||
}
|
||||
setState(() {
|
||||
_currentStepIndex = _steps.length; // 进入完成态
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import '../models/app_settings.dart';
|
||||
import '../services/storage_service.dart';
|
||||
import '../services/tts_service.dart';
|
||||
import '../services/audio_service.dart';
|
||||
import 'plan_config_screen.dart';
|
||||
import 'lan_sync_screen.dart';
|
||||
|
||||
@@ -181,7 +182,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
children: [
|
||||
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.enableSound,
|
||||
activeTrackColor: const Color(0xFF2563EB),
|
||||
onChanged: (val) {
|
||||
@@ -189,6 +190,26 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
_save();
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 8),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
AudioService().playSessionComplete();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('已播放用药完成提示铃声 🔔'), duration: Duration(seconds: 1)),
|
||||
);
|
||||
},
|
||||
style: OutlinedButton.styleFrom(
|
||||
visualDensity: VisualDensity.compact,
|
||||
side: const BorderSide(color: Color(0xFFBFDBFE)),
|
||||
),
|
||||
icon: const Icon(Icons.music_note, size: 16, color: Color(0xFF2563EB)),
|
||||
label: const Text('🔔 点击试听用药完成提示音', style: TextStyle(fontSize: 12, color: Color(0xFF2563EB))),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
SwitchListTile(
|
||||
title: const Text('触觉震动', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class AudioService {
|
||||
static final AudioService _instance = AudioService._internal();
|
||||
factory AudioService() => _instance;
|
||||
AudioService._internal();
|
||||
|
||||
final AudioPlayer _player = AudioPlayer();
|
||||
|
||||
Future<void> init() async {
|
||||
try {
|
||||
await _player.setReleaseMode(ReleaseMode.stop);
|
||||
} catch (e) {
|
||||
debugPrint('AudioService init error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> playStepComplete() async {
|
||||
try {
|
||||
await _player.stop();
|
||||
await _player.play(AssetSource('audio/step_complete.wav'));
|
||||
} catch (e) {
|
||||
debugPrint('Error playing step_complete: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> playSessionComplete() async {
|
||||
try {
|
||||
await _player.stop();
|
||||
await _player.play(AssetSource('audio/session_complete.wav'));
|
||||
} catch (e) {
|
||||
debugPrint('Error playing session_complete: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> dispose() async {
|
||||
try {
|
||||
await _player.dispose();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user