mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
mobile: Share database with playback service
This commit is contained in:
parent
5300d674b5
commit
7c9ac4e0ae
2 changed files with 49 additions and 15 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:moor/isolate.dart';
|
import 'package:moor/isolate.dart';
|
||||||
|
|
@ -88,8 +89,6 @@ class BackendState extends State<Backend> {
|
||||||
MusicusClient client;
|
MusicusClient client;
|
||||||
MusicLibrary ml;
|
MusicLibrary ml;
|
||||||
|
|
||||||
MoorIsolate _moorIsolate;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
@ -105,11 +104,22 @@ class BackendState extends State<Backend> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _load() async {
|
Future<void> _load() async {
|
||||||
_moorIsolate = await _createMoorIsolate();
|
MoorIsolate moorIsolate;
|
||||||
final dbConnection = await _moorIsolate.connect();
|
|
||||||
player.setup();
|
final moorPort = IsolateNameServer.lookupPortByName('moorPort');
|
||||||
|
if (moorPort != null) {
|
||||||
|
moorIsolate = MoorIsolate.fromConnectPort(moorPort);
|
||||||
|
} else {
|
||||||
|
moorIsolate = await _createMoorIsolate();
|
||||||
|
IsolateNameServer.registerPortWithName(
|
||||||
|
moorIsolate.connectPort, 'moorPort');
|
||||||
|
}
|
||||||
|
|
||||||
|
final dbConnection = await moorIsolate.connect();
|
||||||
db = Database.connect(dbConnection);
|
db = Database.connect(dbConnection);
|
||||||
|
|
||||||
|
player.setup();
|
||||||
|
|
||||||
await settings.load();
|
await settings.load();
|
||||||
|
|
||||||
_updateMusicLibrary(settings.musicLibraryUri.value);
|
_updateMusicLibrary(settings.musicLibraryUri.value);
|
||||||
|
|
@ -149,7 +159,6 @@ class BackendState extends State<Backend> {
|
||||||
void dispose() {
|
void dispose() {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
client.dispose();
|
client.dispose();
|
||||||
_moorIsolate.shutdownAll();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import 'dart:isolate';
|
||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:audio_service/audio_service.dart';
|
import 'package:audio_service/audio_service.dart';
|
||||||
|
import 'package:moor/isolate.dart';
|
||||||
|
import 'package:musicus_database/musicus_database.dart';
|
||||||
import 'package:musicus_player/musicus_player.dart';
|
import 'package:musicus_player/musicus_player.dart';
|
||||||
import 'package:rxdart/rxdart.dart';
|
import 'package:rxdart/rxdart.dart';
|
||||||
|
|
||||||
|
|
@ -334,16 +336,11 @@ class _PlaybackService extends BackgroundAudioTask {
|
||||||
action: MediaAction.stop,
|
action: MediaAction.stop,
|
||||||
);
|
);
|
||||||
|
|
||||||
static const dummyMediaItem = MediaItem(
|
|
||||||
id: 'dummy',
|
|
||||||
album: 'Johannes Brahms',
|
|
||||||
title: 'Symphony No. 1 in C minor, Op. 68: 1. Un poco sostenuto — Allegro',
|
|
||||||
duration: 10000,
|
|
||||||
);
|
|
||||||
|
|
||||||
final _completer = Completer();
|
final _completer = Completer();
|
||||||
|
final _loading = Completer();
|
||||||
final List<InternalTrack> _playlist = [];
|
final List<InternalTrack> _playlist = [];
|
||||||
|
|
||||||
|
Database db;
|
||||||
MusicusPlayer _player;
|
MusicusPlayer _player;
|
||||||
int _currentTrack = 0;
|
int _currentTrack = 0;
|
||||||
bool _playing = false;
|
bool _playing = false;
|
||||||
|
|
@ -360,6 +357,16 @@ class _PlaybackService extends BackgroundAudioTask {
|
||||||
_setState();
|
_setState();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_load();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize database.
|
||||||
|
Future<void> _load() async {
|
||||||
|
final moorPort = IsolateNameServer.lookupPortByName('moorPort');
|
||||||
|
final moorIsolate = MoorIsolate.fromConnectPort(moorPort);
|
||||||
|
db = Database.connect(await moorIsolate.connect());
|
||||||
|
_loading.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the audio service status for the system.
|
/// Update the audio service status for the system.
|
||||||
|
|
@ -376,7 +383,25 @@ class _PlaybackService extends BackgroundAudioTask {
|
||||||
updateTime: updateTime,
|
updateTime: updateTime,
|
||||||
);
|
);
|
||||||
|
|
||||||
AudioServiceBackground.setMediaItem(dummyMediaItem);
|
if (_playlist.isNotEmpty) {
|
||||||
|
await _loading.future;
|
||||||
|
|
||||||
|
final track = _playlist[_currentTrack];
|
||||||
|
final recordingInfo = await db.getRecording(track.track.recordingId);
|
||||||
|
final workInfo = await db.getWork(recordingInfo.recording.work);
|
||||||
|
|
||||||
|
final composers = workInfo.composers
|
||||||
|
.map((p) => '${p.firstName} ${p.lastName}')
|
||||||
|
.join(', ');
|
||||||
|
|
||||||
|
final title = workInfo.work.title;
|
||||||
|
|
||||||
|
AudioServiceBackground.setMediaItem(MediaItem(
|
||||||
|
id: track.uri,
|
||||||
|
album: composers,
|
||||||
|
title: title,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a message to the UI.
|
/// Send a message to the UI.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue