## Critical Fixes - Fix CRITICAL-1: Empty scans no longer delete entire library - Use timestamp-based tombstone logic instead of path-based - Only mark missing if livePaths is not empty - Fix CRITICAL-2: Re-scanned files now return from deleted state - Add deleted=false to SongsCompanion.insert in both scanners ## High Priority Fixes - HIGH-1: Fix SQLite variable limit crash on large libraries - Replace isNotIn() with timestamp comparison (O(1) not O(n)) - HIGH-2: Fix UI freezing during scan - Throttle progress updates (every 50 files or end of scan) - HIGH-3: Add error handling for scan failures - Wrap scanFolders/scanAndroidMediaStore in try/catch - Display scanError banner in LibraryScreen - HIGH-4: Add error handling for playback failures - Wrap loadPlaylist in try/catch ## Medium Priority Fixes - MEDIUM-1: Replace ! with ?? to handle unknown ProcessingState - MEDIUM-2: Escape LIKE wildcards in search queries - MEDIUM-4: Display StreamBuilder errors instead of treating as empty - MEDIUM-8: Add tooltips to all IconButtons for accessibility - Add doc comments to public database APIs ## Testing - Add regression tests for CRITICAL-1 and CRITICAL-2 - All 9 existing tests pass Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
119 lines
3.9 KiB
Dart
119 lines
3.9 KiB
Dart
import 'package:audio_service/audio_service.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
/// Kern der Wiedergabe: kapselt just_audio hinter audio_service,
|
|
/// damit Hintergrund-Wiedergabe + Lockscreen/Notification funktionieren.
|
|
class MeloAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|
final AudioPlayer _player = AudioPlayer();
|
|
|
|
MeloAudioHandler() {
|
|
// just_audio-Events → audio_service PlaybackState
|
|
_player.playbackEventStream.map(_transformEvent).pipe(playbackState);
|
|
|
|
// Aktuellen Track ans System melden (Lockscreen/Notification).
|
|
// Bereits bekannte Dauer erhalten, damit sie nicht auf null zurückfällt.
|
|
_player.currentIndexStream.listen((index) {
|
|
final q = queue.value;
|
|
if (index != null && index < q.length) {
|
|
mediaItem.add(q[index].copyWith(duration: _player.duration));
|
|
}
|
|
});
|
|
|
|
// Ermittelte Dauer nachtragen
|
|
_player.durationStream.listen((duration) {
|
|
final item = mediaItem.value;
|
|
if (item != null && duration != null) {
|
|
mediaItem.add(item.copyWith(duration: duration));
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Ersetzt die Warteschlange und startet ab [startIndex].
|
|
Future<void> loadPlaylist(List<MediaItem> items, {int startIndex = 0}) async {
|
|
queue.add(items);
|
|
final sources = items
|
|
.map((item) => AudioSource.uri(Uri.parse(item.id), tag: item))
|
|
.toList();
|
|
try {
|
|
await _player.setAudioSources(sources, initialIndex: startIndex);
|
|
await play();
|
|
} catch (e) {
|
|
playbackState.addError(e);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> play() => _player.play();
|
|
|
|
@override
|
|
Future<void> pause() => _player.pause();
|
|
|
|
@override
|
|
Future<void> stop() async {
|
|
await _player.stop();
|
|
await super.stop();
|
|
}
|
|
|
|
@override
|
|
Future<void> seek(Duration position) => _player.seek(position);
|
|
|
|
@override
|
|
Future<void> skipToNext() => _player.seekToNext();
|
|
|
|
@override
|
|
Future<void> skipToPrevious() => _player.seekToPrevious();
|
|
|
|
@override
|
|
Future<void> skipToQueueItem(int index) => _player.seek(Duration.zero, index: index);
|
|
|
|
@override
|
|
Future<void> setShuffleMode(AudioServiceShuffleMode shuffleMode) async {
|
|
final enabled = shuffleMode == AudioServiceShuffleMode.all;
|
|
if (enabled) await _player.shuffle();
|
|
await _player.setShuffleModeEnabled(enabled);
|
|
}
|
|
|
|
@override
|
|
Future<void> setRepeatMode(AudioServiceRepeatMode repeatMode) {
|
|
return _player.setLoopMode(switch (repeatMode) {
|
|
AudioServiceRepeatMode.one => LoopMode.one,
|
|
AudioServiceRepeatMode.all => LoopMode.all,
|
|
_ => LoopMode.off,
|
|
});
|
|
}
|
|
|
|
PlaybackState _transformEvent(PlaybackEvent event) {
|
|
return PlaybackState(
|
|
controls: [
|
|
MediaControl.skipToPrevious,
|
|
if (_player.playing) MediaControl.pause else MediaControl.play,
|
|
MediaControl.skipToNext,
|
|
],
|
|
systemActions: const {
|
|
MediaAction.seek,
|
|
MediaAction.seekForward,
|
|
MediaAction.seekBackward,
|
|
},
|
|
androidCompactActionIndices: const [0, 1, 2],
|
|
processingState: const {
|
|
ProcessingState.idle: AudioProcessingState.idle,
|
|
ProcessingState.loading: AudioProcessingState.loading,
|
|
ProcessingState.buffering: AudioProcessingState.buffering,
|
|
ProcessingState.ready: AudioProcessingState.ready,
|
|
ProcessingState.completed: AudioProcessingState.completed,
|
|
}[_player.processingState] ??
|
|
AudioProcessingState.idle,
|
|
playing: _player.playing,
|
|
updatePosition: _player.position,
|
|
bufferedPosition: _player.bufferedPosition,
|
|
speed: _player.speed,
|
|
queueIndex: event.currentIndex,
|
|
);
|
|
}
|
|
|
|
// Streams, die die UI beobachtet. Dauer direkt vom Player — zuverlässiger
|
|
// als mediaItem.duration, das beim Track-Wechsel kurzzeitig null sein kann.
|
|
Stream<Duration> get positionStream => _player.positionStream;
|
|
Stream<Duration?> get durationStream => _player.durationStream;
|
|
}
|