为 audioplayers_android 模块单独设置 Java 1.8,其他模块保持 Java 17

This commit is contained in:
Developer
2026-03-31 03:13:47 +08:00
parent 7c09ade2bb
commit d66dc73349
24 changed files with 367 additions and 527 deletions

View File

@@ -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');
}
}
}

View File

@@ -26,13 +26,6 @@ class HttpClient {
static final Dio _dio = Dio(_options);
/// 添加调试日志
static void _debugLog(String message) {
if (kDebugMode) {
print('HttpClient: $message');
}
}
/// GET请求
static Future<HttpResponse> get(
String path, {
@@ -92,10 +85,6 @@ class HttpClient {
}) async {
try {
final url = '$_baseUrl$path';
_debugLog('请求 $method $url');
if (queryParameters != null) {
_debugLog('查询参数: $queryParameters');
}
final options = Options(
method: method,
@@ -129,9 +118,6 @@ class HttpClient {
throw UnsupportedError('HTTP method $method is not supported');
}
_debugLog('响应状态: ${response.statusCode}');
_debugLog('响应数据: ${response.data}');
return HttpResponse(
statusCode: response.statusCode ?? 0,
body: response.data is String
@@ -140,7 +126,6 @@ class HttpClient {
headers: response.headers.map.cast<String, String>(),
);
} on DioException catch (e) {
_debugLog('Dio异常: ${e.type} - ${e.message}');
String message;
switch (e.type) {
case DioExceptionType.connectionTimeout:
@@ -159,7 +144,6 @@ class HttpClient {
}
throw HttpException(message);
} catch (e) {
_debugLog('未知异常: $e');
throw HttpException('请求失败:$e');
}
}
@@ -175,13 +159,6 @@ class HttpClient {
}) async {
try {
final url = '$_baseUrl$path';
_debugLog('FormData请求 $method $url');
if (queryParameters != null) {
_debugLog('查询参数: $queryParameters');
}
if (data != null) {
_debugLog('表单数据: $data');
}
final formData = FormData.fromMap(data ?? {});
@@ -213,9 +190,6 @@ class HttpClient {
throw UnsupportedError('FormData only supports POST method');
}
_debugLog('响应状态: ${response.statusCode}');
_debugLog('响应数据: ${response.data}');
return HttpResponse(
statusCode: response.statusCode ?? 0,
body: response.data is String
@@ -224,7 +198,6 @@ class HttpClient {
headers: response.headers.map.cast<String, String>(),
);
} on DioException catch (e) {
_debugLog('Dio异常: ${e.type} - ${e.message}');
String message;
switch (e.type) {
case DioExceptionType.connectionTimeout:
@@ -243,7 +216,6 @@ class HttpClient {
}
throw HttpException(message);
} catch (e) {
_debugLog('未知异常: $e');
throw HttpException('请求失败:$e');
}
}

View File

@@ -269,11 +269,6 @@ class PoetryData {
});
factory PoetryData.fromJson(Map<String, dynamic> json) {
// 添加调试信息
if (kDebugMode) {
print('PoetryData.fromJson: 输入JSON = $json');
}
try {
final poetryData = PoetryData(
id: int.tryParse(json['id'].toString()) ?? 0,
@@ -295,16 +290,9 @@ class PoetryData {
createTime: json['create_time']?.toString() ?? '',
updateTime: json['update_time']?.toString() ?? '',
);
if (kDebugMode) {
print('PoetryData.fromJson: 解析成功');
}
return poetryData;
} catch (e) {
if (kDebugMode) {
print('PoetryData.fromJson: 解析失败 - $e');
}
rethrow;
}
}

View File

@@ -34,32 +34,19 @@ class VoteApi {
static final Dio _dio = Dio(_options)
..interceptors.add(CookieManager(_cookieJar));
static void _debugLog(String message) {
if (kDebugMode) {
print('VoteApi: $message');
}
}
static Future<Map<String, dynamic>> _get(
String path, {
Map<String, dynamic>? queryParameters,
}) async {
try {
final url = '$_baseUrl$path';
_debugLog('GET $url');
if (queryParameters != null) {
_debugLog('查询参数: $queryParameters');
}
final response = await _dio.get(url, queryParameters: queryParameters);
_debugLog('响应: ${response.data}');
return response.data as Map<String, dynamic>;
} on DioException catch (e) {
_debugLog('Dio异常: ${e.type} - ${e.message}');
throw Exception('请求失败: ${e.message}');
} catch (e) {
_debugLog('未知异常: $e');
throw Exception('请求失败: $e');
}
}
@@ -70,20 +57,13 @@ class VoteApi {
}) async {
try {
final url = '$_baseUrl$path';
_debugLog('POST $url');
if (data != null) {
_debugLog('请求数据: $data');
}
final response = await _dio.post(url, data: data);
_debugLog('响应: ${response.data}');
return response.data as Map<String, dynamic>;
} on DioException catch (e) {
_debugLog('Dio异常: ${e.type} - ${e.message}');
throw Exception('请求失败: ${e.message}');
} catch (e) {
_debugLog('未知异常: $e');
throw Exception('请求失败: $e');
}
}