为 audioplayers_android 模块单独设置 Java 1.8,其他模块保持 Java 17
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// 时间: 2026-03-30
|
||||
/// 功能: 音频管理类
|
||||
/// 介绍: 管理应用中的音效播放,包括点击音效、点赞音效等
|
||||
/// 最新变化: 新增音频播放功能
|
||||
/// 最新变化: 使用最简单的音频播放方式
|
||||
|
||||
class AudioManager {
|
||||
static AudioManager? _instance;
|
||||
late AudioPlayer _audioPlayer;
|
||||
bool _isInitialized = false;
|
||||
bool _isMuted = false;
|
||||
|
||||
AudioManager._internal() {
|
||||
_audioPlayer = AudioPlayer();
|
||||
}
|
||||
AudioManager._internal();
|
||||
|
||||
factory AudioManager() {
|
||||
_instance ??= AudioManager._internal();
|
||||
@@ -25,73 +23,64 @@ class AudioManager {
|
||||
if (_isInitialized) return;
|
||||
|
||||
try {
|
||||
// 设置音频播放器模式
|
||||
await _audioPlayer.setPlayerMode(PlayerMode.lowLatency);
|
||||
await _loadSoundSetting();
|
||||
_isInitialized = true;
|
||||
_debugLog('音频管理器初始化成功');
|
||||
} catch (e) {
|
||||
_debugLog('音频管理器初始化失败: $e');
|
||||
// 初始化失败
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载保存的声音设置
|
||||
Future<void> _loadSoundSetting() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
bool soundEnabled = prefs.getBool('sound_enabled') ?? false;
|
||||
_isMuted = !soundEnabled;
|
||||
} catch (e) {
|
||||
// 加载设置失败
|
||||
}
|
||||
}
|
||||
|
||||
/// 播放点击音效
|
||||
Future<void> playClickSound() async {
|
||||
if (_isMuted) return;
|
||||
await _playSound('audios/deep.mp3');
|
||||
void playClickSound() {
|
||||
_playSound('audios/deep.mp3');
|
||||
}
|
||||
|
||||
/// 播放点赞音效
|
||||
Future<void> playLikeSound() async {
|
||||
if (_isMuted) return;
|
||||
await _playSound('audios/deep.mp3');
|
||||
void playLikeSound() {
|
||||
_playSound('audios/deep.mp3');
|
||||
}
|
||||
|
||||
/// 播放下一条音效
|
||||
Future<void> playNextSound() async {
|
||||
if (_isMuted) return;
|
||||
await _playSound('audios/deep.mp3');
|
||||
void playNextSound() {
|
||||
_playSound('audios/deep.mp3');
|
||||
}
|
||||
|
||||
/// 通用播放方法
|
||||
Future<void> _playSound(String assetPath) async {
|
||||
if (!_isInitialized) {
|
||||
await init();
|
||||
}
|
||||
/// 播放音频
|
||||
void _playSound(String assetPath) {
|
||||
if (_isMuted) return;
|
||||
_playSoundAsync(assetPath);
|
||||
}
|
||||
|
||||
/// 异步播放音频
|
||||
void _playSoundAsync(String assetPath) async {
|
||||
try {
|
||||
// 停止当前播放的音频
|
||||
await _audioPlayer.stop();
|
||||
// 播放新音频
|
||||
await _audioPlayer.play(AssetSource(assetPath));
|
||||
_debugLog('播放音效: $assetPath');
|
||||
final player = AudioPlayer();
|
||||
await player.play(AssetSource(assetPath));
|
||||
|
||||
player.onPlayerComplete.listen((_) {
|
||||
player.dispose();
|
||||
});
|
||||
} catch (e) {
|
||||
_debugLog('播放音效失败: $e');
|
||||
// 播放失败
|
||||
}
|
||||
}
|
||||
|
||||
/// 设置静音状态
|
||||
void setMuted(bool muted) {
|
||||
_isMuted = muted;
|
||||
_debugLog('静音状态: $_isMuted');
|
||||
}
|
||||
|
||||
/// 获取静音状态
|
||||
bool get isMuted => _isMuted;
|
||||
|
||||
/// 释放资源
|
||||
Future<void> dispose() async {
|
||||
try {
|
||||
await _audioPlayer.dispose();
|
||||
_isInitialized = false;
|
||||
_debugLog('音频管理器已释放');
|
||||
} catch (e) {
|
||||
_debugLog('释放音频管理器失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _debugLog(String message) {
|
||||
if (kDebugMode) {
|
||||
print('[AudioManager] $message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user