From 7e7df4dc46d781a18ad7b4830fdca94f3fe6ba86 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 27 Aug 2024 23:54:53 +0800 Subject: [PATCH] :sparkles: Buffered progress display --- lib/screens/player/view.dart | 21 +++++++++++++++------ lib/widgets/player/bottom_player.dart | 10 ++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/screens/player/view.dart b/lib/screens/player/view.dart index bcfd40c..2f2d770 100644 --- a/lib/screens/player/view.dart +++ b/lib/screens/player/view.dart @@ -13,12 +13,13 @@ import 'package:rhythm_box/services/audio_services/image.dart'; import 'package:rhythm_box/widgets/tracks/querying_track_info.dart'; class PlayerScreen extends StatefulWidget { - final Duration durationCurrent, durationTotal; + final Duration durationCurrent, durationTotal, durationBuffered; const PlayerScreen({ super.key, required this.durationCurrent, required this.durationTotal, + required this.durationBuffered, }); @override @@ -38,6 +39,8 @@ class _PlayerScreenState extends State { bool get _isPlaying => _playback.isPlaying.value; bool get _isFetchingActiveTrack => _query.isQueryingTrackInfo.value; + double _bufferProgress = 0; + Duration _durationCurrent = Duration.zero; Duration _durationTotal = Duration.zero; @@ -75,9 +78,14 @@ class _PlayerScreenState extends State { super.initState(); _durationCurrent = widget.durationCurrent; _durationTotal = widget.durationTotal; + _bufferProgress = widget.durationBuffered.inMilliseconds.toDouble(); _subscriptions = [ - audioPlayer.durationStream.listen(_updateDurationTotal), - audioPlayer.positionStream.listen(_updateDurationCurrent), + audioPlayer.durationStream + .listen((dur) => setState(() => _durationTotal = dur)), + audioPlayer.positionStream + .listen((dur) => setState(() => _durationCurrent = dur)), + audioPlayer.bufferedPositionStream.listen((dur) => + setState(() => _bufferProgress = dur.inMilliseconds.toDouble())), ]; } @@ -154,13 +162,14 @@ class _PlayerScreenState extends State { overlayShape: SliderComponentShape.noOverlay, ), child: Slider( - value: _draggingValue ?? + secondaryTrackValue: _bufferProgress.abs(), + value: _draggingValue?.abs() ?? (_durationCurrent.inMilliseconds <= _durationTotal.inMilliseconds - ? _durationCurrent.inMilliseconds.toDouble() + ? _durationCurrent.inMilliseconds.toDouble().abs() : 0), min: 0, - max: _durationTotal.inMilliseconds.toDouble(), + max: _durationTotal.inMilliseconds.abs().toDouble(), onChanged: (value) { setState(() => _draggingValue = value); }, diff --git a/lib/widgets/player/bottom_player.dart b/lib/widgets/player/bottom_player.dart index e86bb65..91f353f 100644 --- a/lib/widgets/player/bottom_player.dart +++ b/lib/widgets/player/bottom_player.dart @@ -45,6 +45,7 @@ class _BottomPlayerState extends State Duration _durationCurrent = Duration.zero; Duration _durationTotal = Duration.zero; + Duration _durationBuffered = Duration.zero; void _updateDurationCurrent(Duration dur) { setState(() => _durationCurrent = dur); @@ -70,8 +71,12 @@ class _BottomPlayerState extends State void initState() { super.initState(); _subscriptions = [ - audioPlayer.durationStream.listen(_updateDurationTotal), - audioPlayer.positionStream.listen(_updateDurationCurrent), + audioPlayer.durationStream + .listen((dur) => setState(() => _durationTotal = dur)), + audioPlayer.positionStream + .listen((dur) => setState(() => _durationCurrent = dur)), + audioPlayer.bufferedPositionStream + .listen((dur) => setState(() => _durationBuffered = dur)), _playback.state.listen((state) { if (state.playlist.medias.isNotEmpty && !_isLifted) { _animationController.animateTo(1); @@ -183,6 +188,7 @@ class _BottomPlayerState extends State context.pushTransparentRoute(PlayerScreen( durationCurrent: _durationCurrent, durationTotal: _durationTotal, + durationBuffered: _durationBuffered, )); }, ),