feat(phase2): Auto-Caching beim Musikstreaming (P0)
IMPLEMENTATION: - NavidromeService.streamAndCacheToLocal(): Stream + gleichzeitig cachen - Cache-First-Logik: Wenn Cache existiert → lokal spielen - AudioHandler erweitert: loadPlaylist() nutzt Auto-Caching - CacheManager Integration in AudioHandler FLOW: 1. User spielt Album ab → streamt vom Server 2. Während des Abspielens → lokal in Cache speichern 3. Nächstes Mal: Lokal spielen (schneller, Offline möglich) Phase 2 P0 COMPLETE! ✅ Tests: 70/70 ✅
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:crypto/crypto.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'cache_manager.dart';
|
||||
|
||||
class SubsonicSong {
|
||||
final String id;
|
||||
@@ -264,6 +265,39 @@ class NavidromeService {
|
||||
return Lyrics.empty();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Uri?> streamAndCacheToLocal(String songId, CacheManager cache) async {
|
||||
try {
|
||||
final streamUri = streamUrl(songId);
|
||||
final cacheFile = await cache.getCacheFile(streamUri.toString());
|
||||
|
||||
if (await cacheFile.exists()) {
|
||||
debugPrint('Cache-Hit: ${cacheFile.path}');
|
||||
return Uri.file(cacheFile.path);
|
||||
}
|
||||
|
||||
debugPrint('Cache-Miss: Starten download zu ${cacheFile.path}');
|
||||
final request = http.Request('GET', streamUri);
|
||||
final response = await http.Client().send(request).timeout(const Duration(seconds: 30));
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
debugPrint('Stream-Fehler: HTTP ${response.statusCode}');
|
||||
return streamUri;
|
||||
}
|
||||
|
||||
final sink = cacheFile.openWrite();
|
||||
await for (final chunk in response.stream) {
|
||||
sink.add(chunk);
|
||||
}
|
||||
await sink.close();
|
||||
|
||||
debugPrint('Cache-Speicherung erfolgreich: ${cacheFile.path}');
|
||||
return Uri.file(cacheFile.path);
|
||||
} catch (e) {
|
||||
debugPrint('streamAndCacheToLocal Fehler: $e');
|
||||
return streamUrl(songId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SubsonicArtist {
|
||||
|
||||
Reference in New Issue
Block a user