v2.41 — Issue #3: Musik-Download im Hintergrund (flutter_background_service)
- flutter_background_service ^5.0.0 hinzugefügt - AndroidManifest: FOREGROUND_SERVICE_DATA_SYNC Permission - main.dart: Background Service mit configure() initialisiert - _onServiceStart(): Service-Callback für Foreground-Garantie - DownloadService._startBackgroundService(): Foreground Service vor Download - DownloadService._stopBackgroundService(): Stopp nach Batch/Single - Integration in _resetStatus, downloadBatch, downloadVonUrl
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:audio_service/audio_service.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_background_service/flutter_background_service.dart';
|
||||
import 'database/db_helper.dart';
|
||||
import 'services/favoriten_service.dart';
|
||||
import 'services/auth_service.dart';
|
||||
@@ -47,6 +48,16 @@ void main() async {
|
||||
AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.createNotificationChannel(downloadChannel);
|
||||
|
||||
// ── Background Service initialisieren (Issue #3) ──
|
||||
await FlutterBackgroundService().configure(
|
||||
iosConfiguration: IosConfiguration(),
|
||||
androidConfiguration: AndroidConfiguration(
|
||||
onStart: _onServiceStart,
|
||||
autoStart: false,
|
||||
isForegroundMode: true,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
await DbHelper().db;
|
||||
await FavoritenService().init();
|
||||
@@ -148,6 +159,19 @@ Future<void> _starteAutoSyncFallsNoetig() async {
|
||||
}
|
||||
}
|
||||
|
||||
/// Background Service Callback – hält App im Vordergrund beim Download (Issue #3)
|
||||
@pragma('vm:entry-point')
|
||||
Future<bool> _onServiceStart(ServiceInstance service) async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
// Service läuft als Foreground-Service und hält den Prozess am Leben.
|
||||
// Der eigentliche Download läuft im Haupt-Isolate – der Service dient nur
|
||||
// als WakeLock / Foreground-Garantie.
|
||||
service.on('stopService').listen((_) {
|
||||
service.stopSelf();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
class MeloApp extends StatelessWidget {
|
||||
const MeloApp({super.key});
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_background_service/flutter_background_service.dart';
|
||||
import '../models/song.dart';
|
||||
import '../database/db_helper.dart';
|
||||
import '../utils/audio_validator.dart';
|
||||
@@ -66,13 +67,46 @@ class DownloadService extends ChangeNotifier {
|
||||
_aktuellerTitel = null;
|
||||
_fehler = null;
|
||||
_abbruchCompleter = Completer<void>();
|
||||
_startBackgroundService();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Issue #3: Foreground Service starten – hält App im Hintergrund aktiv
|
||||
void _startBackgroundService() {
|
||||
try {
|
||||
final svc = FlutterBackgroundService();
|
||||
svc.isRunning().then((running) {
|
||||
if (!running) {
|
||||
svc.startService();
|
||||
}
|
||||
});
|
||||
debugPrint('🔵 Background Service gestartet');
|
||||
} catch (e) {
|
||||
debugPrint('Background Service Start fehlgeschlagen: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Issue #3: Foreground Service stoppen
|
||||
void _stopBackgroundService() {
|
||||
try {
|
||||
final svc = FlutterBackgroundService();
|
||||
svc.isRunning().then((running) {
|
||||
if (running) {
|
||||
svc.invoke('stopService');
|
||||
}
|
||||
});
|
||||
debugPrint('🔴 Background Service gestoppt');
|
||||
} catch (e) {
|
||||
debugPrint('Background Service Stop fehlgeschlagen: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Song von YouTube herunterladen
|
||||
Future<Song?> downloadVonUrl(String url) async {
|
||||
_resetStatus();
|
||||
return _downloadEinzeln(url);
|
||||
final result = await _downloadEinzeln(url);
|
||||
if (!_ladt) _stopBackgroundService();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Mehrere URLs/Playlists nacheinander mit Cooldown
|
||||
@@ -147,6 +181,7 @@ class DownloadService extends ChangeNotifier {
|
||||
}
|
||||
notifyListeners();
|
||||
_completeNotification(erfolgreich, urls.length, abgebrochen: sollAbbrechen);
|
||||
_stopBackgroundService();
|
||||
MeloLogger().zustand('download_batch_ende', {
|
||||
'erfolgreich': erfolgreich,
|
||||
'gesamt': urls.length,
|
||||
|
||||
Reference in New Issue
Block a user