2023-07-29 02:17:26 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:freezer/settings.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2023-09-26 00:06:59 +00:00
|
|
|
import 'package:rxdart/rxdart.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2023-10-18 15:08:05 +00:00
|
|
|
import '../api/player/audio_handler.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'cached_image.dart';
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
class PlayerBar extends StatelessWidget {
|
2023-10-12 22:09:37 +00:00
|
|
|
final VoidCallback? onTap;
|
2023-07-29 02:17:26 +00:00
|
|
|
final bool shouldHaveHero;
|
|
|
|
final Color? backgroundColor;
|
2023-10-12 22:09:37 +00:00
|
|
|
final FocusNode? focusNode;
|
2023-07-29 02:17:26 +00:00
|
|
|
const PlayerBar({
|
|
|
|
Key? key,
|
2023-10-12 22:09:37 +00:00
|
|
|
this.onTap,
|
2023-07-29 02:17:26 +00:00
|
|
|
this.shouldHaveHero = true,
|
|
|
|
this.backgroundColor,
|
2023-10-12 22:09:37 +00:00
|
|
|
this.focusNode,
|
2023-07-29 02:17:26 +00:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final double iconSize = 28;
|
|
|
|
|
|
|
|
double parsePosition(Duration position) {
|
|
|
|
if (audioHandler.mediaItem.value == null) return 0.0;
|
2023-10-12 22:09:37 +00:00
|
|
|
if (audioHandler.mediaItem.value!.duration!.inSeconds == 0) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return 0.0; //Division by 0
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
return position.inSeconds /
|
|
|
|
audioHandler.mediaItem.value!.duration!.inSeconds;
|
|
|
|
}
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
Color? get _backgroundColor => backgroundColor;
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-10-08 10:53:22 +00:00
|
|
|
return SizedBox(
|
|
|
|
height: 68.0,
|
2023-10-16 22:57:55 +00:00
|
|
|
child: Column(mainAxisSize: MainAxisSize.max, children: <Widget>[
|
2023-10-16 22:22:50 +00:00
|
|
|
Expanded(
|
|
|
|
child: StreamBuilder<MediaItem?>(
|
|
|
|
stream: audioHandler.mediaItem,
|
|
|
|
initialData: audioHandler.mediaItem.valueOrNull,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.data == null) {
|
2024-01-24 17:55:25 +00:00
|
|
|
// lazy way to prevent dragging up
|
|
|
|
return GestureDetector(
|
|
|
|
behavior: HitTestBehavior.opaque,
|
|
|
|
onVerticalDragEnd: (_) {},
|
|
|
|
onVerticalDragUpdate: (_) {},
|
|
|
|
child: Material(
|
|
|
|
child: ListTile(
|
|
|
|
dense: true,
|
|
|
|
visualDensity: VisualDensity.standard,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 16.0, vertical: 6.0),
|
|
|
|
leading: Image.asset(
|
|
|
|
'assets/cover_thumb.jpg',
|
|
|
|
width: 48.0,
|
|
|
|
height: 48.0,
|
|
|
|
),
|
|
|
|
title: Text('Nothing is currently playing'.i18n),
|
2023-10-17 14:03:39 +00:00
|
|
|
),
|
2023-10-16 22:22:50 +00:00
|
|
|
),
|
|
|
|
);
|
2023-10-08 10:53:22 +00:00
|
|
|
}
|
2023-10-16 22:22:50 +00:00
|
|
|
final currentMediaItem = snapshot.data!;
|
|
|
|
final image = CachedImage(
|
2024-01-28 11:02:59 +00:00
|
|
|
rounded: true,
|
2023-10-16 22:22:50 +00:00
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
url: currentMediaItem.extras!['thumb'] ??
|
|
|
|
currentMediaItem.artUri.toString(),
|
|
|
|
);
|
|
|
|
final leadingWidget = shouldHaveHero
|
|
|
|
? Hero(tag: currentMediaItem.id, child: image)
|
|
|
|
: image;
|
|
|
|
return Material(
|
2024-01-24 17:55:25 +00:00
|
|
|
type: MaterialType.transparency,
|
2023-10-16 22:22:50 +00:00
|
|
|
child: ListTile(
|
2023-10-17 14:03:39 +00:00
|
|
|
dense: true,
|
2023-10-16 22:22:50 +00:00
|
|
|
tileColor: _backgroundColor,
|
|
|
|
focusNode: focusNode,
|
2023-10-17 14:03:39 +00:00
|
|
|
visualDensity: VisualDensity.standard,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 16.0, vertical: 0.0),
|
2023-10-16 22:22:50 +00:00
|
|
|
onTap: onTap,
|
|
|
|
leading: AnimatedSwitcher(
|
2023-10-16 22:57:55 +00:00
|
|
|
key: const ValueKey('player_bar_art_switcher'),
|
2023-10-16 22:22:50 +00:00
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
child: leadingWidget),
|
|
|
|
title: Text(
|
|
|
|
currentMediaItem.displayTitle!,
|
|
|
|
overflow: TextOverflow.clip,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
currentMediaItem.displaySubtitle ?? '',
|
|
|
|
overflow: TextOverflow.clip,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
trailing: IconTheme(
|
|
|
|
data: IconThemeData(
|
|
|
|
color: settings.isDark
|
|
|
|
? Colors.white
|
|
|
|
: Colors.grey[600]),
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
PrevNextButton(
|
|
|
|
iconSize,
|
|
|
|
prev: true,
|
|
|
|
),
|
|
|
|
PlayPauseButton(iconSize),
|
|
|
|
PrevNextButton(iconSize)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 3.0,
|
|
|
|
child: StreamBuilder<Duration>(
|
|
|
|
stream: AudioService.position,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return LinearProgressIndicator(
|
|
|
|
value: parsePosition(snapshot.data ?? Duration.zero),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
]),
|
2023-10-08 10:53:22 +00:00
|
|
|
);
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PrevNextButton extends StatelessWidget {
|
|
|
|
final double size;
|
|
|
|
final bool prev;
|
|
|
|
final bool hidePrev;
|
2023-10-12 22:09:37 +00:00
|
|
|
const PrevNextButton(this.size,
|
|
|
|
{super.key, this.prev = false, this.hidePrev = false});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-09-26 00:06:59 +00:00
|
|
|
// Listens to both mediaItem updates (a.k.a. when the song changes)
|
|
|
|
// and queue updates (so for example, in SmartTrackLists, when the songs are fetched, it's updated with the new items)
|
|
|
|
return StreamBuilder<bool>(
|
|
|
|
stream: Rx.combineLatest2<MediaItem?, List<MediaItem>, bool>(
|
|
|
|
audioHandler.mediaItem,
|
|
|
|
audioHandler.queue,
|
|
|
|
(_, queue) => playerHelper.queueIndex < queue.length - 1),
|
2023-07-29 02:17:26 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!prev) {
|
|
|
|
return IconButton(
|
2023-10-12 22:09:37 +00:00
|
|
|
splashRadius: size * 2.0,
|
2023-07-29 02:17:26 +00:00
|
|
|
icon: Icon(
|
|
|
|
Icons.skip_next,
|
|
|
|
semanticLabel: "Play next".i18n,
|
|
|
|
),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed:
|
2023-09-26 00:06:59 +00:00
|
|
|
snapshot.data == true ? () => audioHandler.skipToNext() : null,
|
2023-07-29 02:17:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
final canGoPrev = playerHelper.queueIndex > 0;
|
|
|
|
|
2023-09-26 00:06:59 +00:00
|
|
|
if (!canGoPrev && hidePrev) return const SizedBox.shrink();
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
return IconButton(
|
2023-10-12 22:09:37 +00:00
|
|
|
splashRadius: size * 2.0,
|
2023-07-29 02:17:26 +00:00
|
|
|
icon: Icon(
|
|
|
|
Icons.skip_previous,
|
|
|
|
semanticLabel: "Play previous".i18n,
|
|
|
|
),
|
|
|
|
iconSize: size,
|
|
|
|
onPressed: canGoPrev ? () => audioHandler.skipToPrevious() : null,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PlayPauseButton extends StatefulWidget {
|
|
|
|
final double size;
|
|
|
|
final bool filled;
|
2024-01-28 11:02:59 +00:00
|
|
|
final bool material3;
|
2023-07-29 02:17:26 +00:00
|
|
|
final Color? iconColor;
|
|
|
|
|
|
|
|
/// The color of the card if [filled] is true
|
|
|
|
final Color? color;
|
|
|
|
const PlayPauseButton(
|
|
|
|
this.size, {
|
|
|
|
Key? key,
|
|
|
|
this.filled = false,
|
2024-01-28 11:02:59 +00:00
|
|
|
this.material3 = true,
|
2023-07-29 02:17:26 +00:00
|
|
|
this.color,
|
|
|
|
this.iconColor,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
2023-10-12 22:09:37 +00:00
|
|
|
State<PlayPauseButton> createState() => _PlayPauseButtonState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _PlayPauseButtonState extends State<PlayPauseButton>
|
|
|
|
with SingleTickerProviderStateMixin {
|
2023-10-14 14:26:13 +00:00
|
|
|
late final AnimationController _controller;
|
|
|
|
late final Animation<double> _animation;
|
2024-01-28 11:02:59 +00:00
|
|
|
late StreamSubscription _stateSubscription;
|
|
|
|
late StreamSubscription _playingSubscription;
|
2023-10-15 14:48:55 +00:00
|
|
|
late bool _canPlay = audioHandler.playbackState.value.processingState ==
|
|
|
|
AudioProcessingState.ready ||
|
2023-07-29 02:17:26 +00:00
|
|
|
audioHandler.playbackState.value.processingState ==
|
2023-10-15 14:48:55 +00:00
|
|
|
AudioProcessingState.idle;
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2023-10-14 14:26:13 +00:00
|
|
|
_controller = AnimationController(
|
2023-10-15 14:48:55 +00:00
|
|
|
vsync: this, duration: const Duration(milliseconds: 200));
|
2023-10-14 14:26:13 +00:00
|
|
|
_animation = CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
|
2023-10-15 14:48:55 +00:00
|
|
|
|
2024-01-28 11:02:59 +00:00
|
|
|
_stateSubscription = playerHelper.processingState.listen((processingState) {
|
|
|
|
print('yes!');
|
|
|
|
if (processingState == AudioProcessingState.ready ||
|
2023-10-15 14:48:55 +00:00
|
|
|
audioHandler.playbackState.value.processingState ==
|
|
|
|
AudioProcessingState.idle) {
|
2023-07-29 02:17:26 +00:00
|
|
|
if (!_canPlay) setState(() => _canPlay = true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() => _canPlay = false);
|
|
|
|
});
|
2024-01-28 11:02:59 +00:00
|
|
|
_playingSubscription = playerHelper.playing.listen((playing) {
|
|
|
|
if (playing) {
|
|
|
|
_controller.forward();
|
|
|
|
} else {
|
|
|
|
_controller.reverse();
|
|
|
|
}
|
|
|
|
});
|
2023-07-29 02:17:26 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-01-28 11:02:59 +00:00
|
|
|
_stateSubscription.cancel();
|
|
|
|
_playingSubscription.cancel();
|
2023-07-29 02:17:26 +00:00
|
|
|
_controller.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _playPause() {
|
2023-10-12 22:09:37 +00:00
|
|
|
if (audioHandler.playbackState.value.playing) {
|
2023-07-29 02:17:26 +00:00
|
|
|
audioHandler.pause();
|
2023-10-12 22:09:37 +00:00
|
|
|
} else {
|
2023-07-29 02:17:26 +00:00
|
|
|
audioHandler.play();
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final Widget? child;
|
|
|
|
if (_canPlay) {
|
|
|
|
final icon = AnimatedIcon(
|
|
|
|
icon: AnimatedIcons.play_pause,
|
|
|
|
progress: _animation,
|
|
|
|
semanticLabel: audioHandler.playbackState.value.playing
|
|
|
|
? 'Pause'.i18n
|
|
|
|
: 'Play'.i18n,
|
|
|
|
);
|
2023-10-12 22:09:37 +00:00
|
|
|
if (!widget.filled) {
|
2024-01-28 11:02:59 +00:00
|
|
|
child = IconButton(
|
2023-07-29 02:17:26 +00:00
|
|
|
color: widget.iconColor,
|
|
|
|
icon: icon,
|
|
|
|
iconSize: widget.size,
|
|
|
|
onPressed: _playPause);
|
2023-10-12 22:09:37 +00:00
|
|
|
} else {
|
|
|
|
child = Material(
|
|
|
|
type: MaterialType.transparency,
|
|
|
|
child: InkWell(
|
2024-01-28 11:02:59 +00:00
|
|
|
customBorder: widget.material3 ? null : const CircleBorder(),
|
2023-10-12 22:09:37 +00:00
|
|
|
onTap: _playPause,
|
|
|
|
child: IconTheme.merge(
|
|
|
|
child: Center(child: icon),
|
|
|
|
data: IconThemeData(
|
|
|
|
size: widget.size / 2, color: widget.iconColor))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-29 02:17:26 +00:00
|
|
|
switch (audioHandler.playbackState.value.processingState) {
|
|
|
|
//Stopped/Error
|
|
|
|
case AudioProcessingState.error:
|
|
|
|
child = null;
|
|
|
|
break;
|
|
|
|
//Loading, connecting, rewinding...
|
|
|
|
default:
|
|
|
|
child = const Center(child: CircularProgressIndicator());
|
|
|
|
break;
|
|
|
|
}
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2024-01-28 11:02:59 +00:00
|
|
|
if (widget.material3 && widget.filled) {
|
|
|
|
return StreamBuilder<bool>(
|
|
|
|
stream: playerHelper.playing,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return AnimatedContainer(
|
|
|
|
clipBehavior: Clip.antiAlias,
|
|
|
|
width: widget.size,
|
|
|
|
height: widget.size,
|
|
|
|
duration: const Duration(milliseconds: 500),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: snapshot.data == true
|
|
|
|
? BorderRadius.circular(24.0)
|
|
|
|
: BorderRadius.circular(36.0),
|
|
|
|
color: widget.color),
|
|
|
|
child: child);
|
|
|
|
});
|
|
|
|
}
|
2023-10-12 22:09:37 +00:00
|
|
|
if (widget.filled) {
|
2024-01-28 11:02:59 +00:00
|
|
|
return AnimatedContainer(
|
|
|
|
width: widget.size,
|
|
|
|
height: widget.size,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
decoration:
|
|
|
|
BoxDecoration(shape: BoxShape.circle, color: widget.color),
|
|
|
|
child: child);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2024-01-28 11:02:59 +00:00
|
|
|
|
|
|
|
return SizedBox.square(dimension: widget.size * 2, child: child);
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|