Files
observer/flutter_app/lib/reminder/reminder.dart
T

35 lines
855 B
Dart

import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:vibration/vibration.dart';
/// 提醒:同类目标 10s 内只提醒一次。
/// 震动/提示音默认开启,不提供关闭入口。
class Reminder {
final AudioPlayer _player = AudioPlayer();
String? _lastAlertLabel;
int _lastAlertAt = 0;
/// 同类目标 10s 内只提醒一次
void onDetected(String label) {
final now = DateTime.now().millisecondsSinceEpoch;
if (label == _lastAlertLabel && now - _lastAlertAt < 10000) return;
_lastAlertAt = now;
_lastAlertLabel = label;
_vibrate();
_playTone();
}
void _vibrate() => Vibration.vibrate(duration: 200);
Future<void> _playTone() async {
await _player.stop();
await _player.play(AssetSource('beep.wav'));
}
void release() {
_player.dispose();
}
}