Functional player bar

This commit is contained in:
Elias Projahn 2020-04-22 10:32:36 +02:00
parent 3f98037160
commit 87838ce7d2
3 changed files with 54 additions and 14 deletions

View file

@ -31,7 +31,12 @@ class Player {
/// Index of the currently played (or paused) track within the playlist.
///
/// This will be zero, if the player is not active!
final currentTrack = BehaviorSubject.seeded(0);
final currentIndex = BehaviorSubject.seeded(0);
/// The currently played track.
///
/// This will be null, if there is no current track.
final currentTrack = BehaviorSubject<InternalTrack>.seeded(null);
/// Whether we are currently playing or not.
///
@ -63,7 +68,7 @@ class Player {
void _stop() {
active.add(false);
playlist.add([]);
currentTrack.add(0);
currentIndex.add(0);
playing.add(false);
position.add(const Duration());
duration.add(const Duration(seconds: 1));
@ -100,7 +105,8 @@ class Player {
// TODO: Consider checking, whether values have actually changed.
playlist.add(state.playlist);
currentTrack.add(state.currentTrack);
currentIndex.add(state.currentTrack);
currentTrack.add(state.playlist[state.currentTrack]);
playing.add(state.playing);
position.add(Duration(milliseconds: state.positionMs));
duration.add(Duration(milliseconds: state.durationMs));
@ -184,6 +190,9 @@ class Player {
void dispose() {
_playbackServiceStateSubscription.cancel();
active.close();
playlist.close();
currentIndex.close();
currentTrack.close();
playing.close();
position.close();
duration.close();

View file

@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import '../backend.dart';
import '../music_library.dart';
import '../widgets/play_pause_button.dart';
import '../widgets/recording_tile.dart';
class ProgramScreen extends StatefulWidget {
@override
@ -53,12 +54,13 @@ class _ProgramScreenState extends State<ProgramScreen> {
if (playlist != null && playlist.isNotEmpty) {
return StreamBuilder<int>(
stream: backend.player.currentTrack,
stream: backend.player.currentIndex,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: playlist.length,
itemBuilder: (context, index) {
final track = playlist[index];
return ListTile(
@ -68,7 +70,9 @@ class _ProgramScreenState extends State<ProgramScreen> {
width: 24.0,
height: 24.0,
),
title: Text(track.track.fileName),
title: RecordingTile(
recordingId: track.track.recordingId,
),
onTap: () {
backend.player.skipTo(index);
},

View file

@ -1,9 +1,12 @@
import 'package:flutter/material.dart';
import 'package:musicus/database.dart';
import '../backend.dart';
import '../music_library.dart';
import '../screens/program.dart';
import 'play_pause_button.dart';
import 'texts.dart';
class PlayerBar extends StatelessWidget {
@override
@ -28,15 +31,39 @@ class PlayerBar extends StatelessWidget {
child: Icon(Icons.keyboard_arrow_up),
),
Expanded(
child: Column(
child: StreamBuilder<InternalTrack>(
stream: backend.player.currentTrack,
builder: (context, snapshot) {
if (snapshot.data != null) {
final recordingId = snapshot.data.track.recordingId;
return FutureBuilder<Recording>(
future:
backend.db.recordingById(recordingId).getSingle(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final workId = snapshot.data.work;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Composer',
style: TextStyle(fontWeight: FontWeight.bold),
DefaultTextStyle.merge(
style:
TextStyle(fontWeight: FontWeight.bold),
child: ComposersText(workId),
),
Text('Work: Movement'),
WorkText(workId),
],
);
} else {
return Container();
}
},
);
} else {
return Container();
}
},
),
),
PlayPauseButton(),