jjsos_autosos_flutter/lib/service/audio_service.dart

49 lines
1.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/foundation.dart';
/// 音频播放服务(对应 Android SoundPoolUtils
///
/// 用 AudioPlayersFlutter 包)替代 Android 的 SoundPool
/// 提供短音效(订单提示、警告音)的低延迟播放能力。
class AudioService {
static final AudioService instance = AudioService._();
AudioService._();
final _player = AudioPlayer();
/// 是否启用声音提醒(用户设置开关)
bool enableSound = true;
/// 初始化播放源(使用 AssetSource
Future<void> _play(String assetPath, {double volume = 1.0}) async {
if (!enableSound) return;
try {
await _player.stop();
await _player.setReleaseMode(ReleaseMode.stop);
await _player.setVolume(volume);
await _player.play(AssetSource(assetPath));
} catch (e) {
debugPrint('AudioService play failed: $e');
}
}
/// 新订单到来(救援平台内部订单)
Future<void> newInsideOrder() => _play('sounds/new_order_inside.mp3');
/// 新订单(啾啾救援订单)
Future<void> newJiuJiuOrder() => _play('sounds/new_order_jiujiu.mp3');
/// 订单被关闭
Future<void> orderClosed() => _play('sounds/order_closed.mp3');
/// 通用警告
Future<void> warning() => _play('sounds/warning.mp3');
/// 自定义音效
Future<void> custom(String assetPath, {double volume = 1.0}) =>
_play(assetPath, volume: volume);
void dispose() {
_player.dispose();
}
}