通知中心封面图显示
部分界面的信息更新 缓存系统初步引入 即将进入播放列表更新
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
// lib/services/audio_player_handler.dart
|
||||
import 'dart:io'; // ⭐ 添加
|
||||
import 'dart:typed_data'; // ⭐ 添加
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:audio_service/audio_service.dart' as audio_service;
|
||||
import '../audio/player_controller.dart';
|
||||
import '../audio/playback_state_manager.dart';
|
||||
import '../services/audio_service.dart';
|
||||
import '../utils/file_provider_utils.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:crypto/crypto.dart';
|
||||
|
||||
class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
final PlayerController _player = PlayerController();
|
||||
@@ -15,18 +19,19 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
String? _currentTitle;
|
||||
String? _currentArtist;
|
||||
|
||||
// ⭐ 唯一的位置变量
|
||||
Duration _currentPosition = Duration.zero;
|
||||
DateTime _lastPublishTime = DateTime.now();
|
||||
static const Duration _publishInterval = Duration(milliseconds: 500);
|
||||
|
||||
// ⭐ 缓存 artwork 文件路径,避免重复写入
|
||||
String? _currentArtworkPath;
|
||||
|
||||
AudioPlayerHandler() {
|
||||
_player.playingStream.listen((playing) {
|
||||
_state.updatePlaying(playing);
|
||||
_publishState();
|
||||
});
|
||||
|
||||
// ⭐ 位置更新:直接赋值给 _currentPosition
|
||||
_player.positionStream.listen((position) {
|
||||
_currentPosition = position;
|
||||
_state.updatePosition(position);
|
||||
@@ -34,7 +39,7 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
final now = DateTime.now();
|
||||
if (now.difference(_lastPublishTime) >= _publishInterval) {
|
||||
_lastPublishTime = now;
|
||||
_updateMediaItemPosition(position);
|
||||
// ⭐ 只保留 _publishStateOnly()
|
||||
_publishStateOnly();
|
||||
}
|
||||
});
|
||||
@@ -54,6 +59,7 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
});
|
||||
}
|
||||
|
||||
// ---- 发布状态 ----
|
||||
void _publishState() {
|
||||
final state = _state.playbackState;
|
||||
playbackState.add(audio_service.PlaybackState(
|
||||
@@ -71,31 +77,112 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
|
||||
void _publishStateOnly() {
|
||||
final current = playbackState.value;
|
||||
|
||||
debugPrint(
|
||||
'📡 [publishStateOnly] position=$_currentPosition, playing=${current.playing}',
|
||||
);
|
||||
'📡 [publishStateOnly] position=$_currentPosition, playing=${current.playing}');
|
||||
|
||||
playbackState.add(
|
||||
audio_service.PlaybackState(
|
||||
controls: current.controls.isNotEmpty
|
||||
? current.controls
|
||||
: _state.playbackState.controls,
|
||||
processingState: _state.playbackState.processingState,
|
||||
playing: current.playing,
|
||||
androidCompactActionIndices: current.androidCompactActionIndices,
|
||||
|
||||
// ⭐ 声明支持 seek
|
||||
systemActions: const {
|
||||
audio_service.MediaAction.seek,
|
||||
},
|
||||
|
||||
updatePosition: _currentPosition,
|
||||
updateTime: DateTime.now(),
|
||||
),
|
||||
);
|
||||
playbackState.add(audio_service.PlaybackState(
|
||||
controls: current.controls.isNotEmpty
|
||||
? current.controls
|
||||
: _state.playbackState.controls,
|
||||
processingState: _state.playbackState.processingState,
|
||||
playing: current.playing,
|
||||
androidCompactActionIndices: current.androidCompactActionIndices,
|
||||
updatePosition: _currentPosition,
|
||||
updateTime: DateTime.now(),
|
||||
systemActions: const {
|
||||
audio_service.MediaAction.seek,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
// ---- 更新媒体信息 ----
|
||||
void _updateMediaItem({
|
||||
required String id,
|
||||
required String title,
|
||||
required String artist,
|
||||
Duration? duration,
|
||||
Uint8List? artwork,
|
||||
}) {
|
||||
debugPrint(
|
||||
'📢 [handler] _updateMediaItem: artwork is ${artwork != null ? 'not null (${artwork.length} bytes)' : 'null'}');
|
||||
_currentId = id;
|
||||
_currentTitle = title;
|
||||
_currentArtist = artist;
|
||||
final position = _player.position;
|
||||
|
||||
debugPrint('📢 [handler] updateMediaItem: $title - $artist');
|
||||
|
||||
// ⭐ 异步处理 artwork(不阻塞主流程)
|
||||
_handleArtwork(id, artwork).then((artUri) {
|
||||
// 如果 artUri 变化,重新推送 MediaItem
|
||||
final current = mediaItem.value;
|
||||
if (current != null && current.artUri != artUri) {
|
||||
debugPrint('📢 [handler] updating artUri: $artUri');
|
||||
mediaItem.add(audio_service.MediaItem(
|
||||
id: current.id,
|
||||
title: current.title,
|
||||
artist: current.artist,
|
||||
duration: current.duration,
|
||||
artUri: artUri,
|
||||
extras: current.extras,
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
// 先推送不带封面图的 MediaItem(让 UI 尽快显示)
|
||||
mediaItem.add(audio_service.MediaItem(
|
||||
id: id,
|
||||
title: title,
|
||||
artist: artist,
|
||||
duration: duration ?? _player.duration,
|
||||
extras: {'position': position.inMilliseconds},
|
||||
));
|
||||
}
|
||||
|
||||
/// 处理封面图:保存到本地并生成 content URI
|
||||
Future<Uri?> _handleArtwork(String id, Uint8List? artwork) async {
|
||||
if (artwork == null || artwork.isEmpty) {
|
||||
_currentArtworkPath = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final artworkDir = Directory('${dir.path}/artworks');
|
||||
if (!await artworkDir.exists()) {
|
||||
await artworkDir.create(recursive: true);
|
||||
}
|
||||
|
||||
// 使用 md5 生成安全的文件名
|
||||
final bytes = utf8.encode(id);
|
||||
final digest = md5.convert(bytes);
|
||||
final fileName = '$digest.jpg';
|
||||
final path = '${artworkDir.path}/$fileName';
|
||||
final file = File(path);
|
||||
|
||||
// 检查文件是否已存在
|
||||
if (await file.exists()) {
|
||||
final existingBytes = await file.readAsBytes();
|
||||
if (existingBytes.length == artwork.length &&
|
||||
existingBytes.hashCode == artwork.hashCode) {
|
||||
_currentArtworkPath = path;
|
||||
return await FileProviderUtils.getContentUri(file);
|
||||
}
|
||||
}
|
||||
|
||||
// 写入新文件
|
||||
await file.writeAsBytes(artwork);
|
||||
_currentArtworkPath = path;
|
||||
debugPrint('📢 [handler] artwork saved: $path');
|
||||
|
||||
return await FileProviderUtils.getContentUri(file);
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [handler] artwork handling failed: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 外部接口 ----
|
||||
void updateNotification({
|
||||
required String id,
|
||||
required String title,
|
||||
@@ -108,66 +195,10 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
artist: artist,
|
||||
artwork: artwork,
|
||||
);
|
||||
_publishState();
|
||||
}
|
||||
|
||||
void _updateMediaItem({
|
||||
required String id,
|
||||
required String title,
|
||||
required String artist,
|
||||
Duration? duration,
|
||||
Uint8List? artwork,
|
||||
}) {
|
||||
_currentId = id;
|
||||
_currentTitle = title;
|
||||
_currentArtist = artist;
|
||||
final position = _player.position;
|
||||
|
||||
debugPrint('📢 [handler] updateMediaItem: $title - $artist');
|
||||
|
||||
// ⭐ 如果有 artwork,保存为临时文件并设置 artUri
|
||||
Uri? artUri;
|
||||
if (artwork != null && artwork.isNotEmpty) {
|
||||
try {
|
||||
final tempDir = Directory.systemTemp;
|
||||
final artPath = '${tempDir.path}/art_${id.hashCode}.jpg';
|
||||
final artFile = File(artPath);
|
||||
artFile.writeAsBytesSync(artwork);
|
||||
artUri = Uri.file(artPath);
|
||||
debugPrint('📢 [handler] artwork saved: $artPath');
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [handler] save artwork failed: $e');
|
||||
}
|
||||
}
|
||||
|
||||
mediaItem.add(audio_service.MediaItem(
|
||||
id: id,
|
||||
title: title,
|
||||
artist: artist,
|
||||
duration: duration ?? _player.duration,
|
||||
artUri: artUri,
|
||||
extras: {
|
||||
'position': position.inMilliseconds,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
void _updateMediaItemPosition(Duration position) {
|
||||
final currentMediaItem = mediaItem.value;
|
||||
if (currentMediaItem != null) {
|
||||
mediaItem.add(audio_service.MediaItem(
|
||||
id: currentMediaItem.id,
|
||||
title: currentMediaItem.title,
|
||||
artist: currentMediaItem.artist,
|
||||
duration: currentMediaItem.duration,
|
||||
artUri: currentMediaItem.artUri,
|
||||
extras: {
|
||||
'position': position.inMilliseconds,
|
||||
...?currentMediaItem.extras,
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 控制命令 ----
|
||||
@override
|
||||
Future<void> play() async {
|
||||
debugPrint('▶️ [handler] play() called');
|
||||
@@ -194,7 +225,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
debugPrint('⏩ [handler] seek() called: $position');
|
||||
await _player.seek(position);
|
||||
_currentPosition = position;
|
||||
_updateMediaItemPosition(position);
|
||||
_publishStateOnly();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user