From 4dee111c73981cf2afdb21e0f790d7f00737d6bb Mon Sep 17 00:00:00 2001 From: Krille Date: Fri, 10 May 2024 09:52:55 +0200 Subject: [PATCH] chore: Revert audioplayer changes --- lib/pages/chat/events/audio_player.dart | 135 ++++++++++-------------- 1 file changed, 57 insertions(+), 78 deletions(-) diff --git a/lib/pages/chat/events/audio_player.dart b/lib/pages/chat/events/audio_player.dart index 35b71b077..ce0285aed 100644 --- a/lib/pages/chat/events/audio_player.dart +++ b/lib/pages/chat/events/audio_player.dart @@ -38,8 +38,8 @@ class AudioPlayerState extends State { StreamSubscription? onPlayerError; String? statusText; - double currentPosition = 0.0; - double maxPosition = 1.0; + int currentPosition = 0; + double maxPosition = 0; MatrixFile? matrixFile; File? audioFile; @@ -113,7 +113,9 @@ class AudioPlayerState extends State { setState(() { statusText = '${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}'; - currentPosition = state.inMilliseconds.toDouble(); + currentPosition = ((state.inMilliseconds.toDouble() / maxPosition) * + AudioPlayerWidget.wavesCount) + .round(); }); if (state.inMilliseconds.toDouble() == maxPosition) { audioPlayer.stop(); @@ -149,14 +151,12 @@ class AudioPlayerState extends State { return '${duration.inMinutes.toString().padLeft(2, '0')}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}'; } - List? _getWaveform() { + List _getWaveform() { final eventWaveForm = widget.event.content .tryGetMap('org.matrix.msc1767.audio') ?.tryGetList('waveform'); - if (eventWaveForm == null || - eventWaveForm.isEmpty || - eventWaveForm.length > 100) { - return null; + if (eventWaveForm == null || eventWaveForm.isEmpty) { + return List.filled(AudioPlayerWidget.wavesCount, 500); } while (eventWaveForm.length < AudioPlayerWidget.wavesCount) { for (var i = 0; i < eventWaveForm.length; i = i + 2) { @@ -172,7 +172,7 @@ class AudioPlayerState extends State { return eventWaveForm.map((i) => i > 1024 ? 1024 : i).toList(); } - late final List? waveform; + late final List waveform; void _toggleSpeed() async { final audioPlayer = this.audioPlayer; @@ -208,9 +208,8 @@ class AudioPlayerState extends State { Widget build(BuildContext context) { final statusText = this.statusText ??= _durationString ?? '00:00'; final audioPlayer = this.audioPlayer; - final waveform = this.waveform; return Padding( - padding: const EdgeInsets.all(16), + padding: const EdgeInsets.all(12.0), child: Row( mainAxisSize: MainAxisSize.min, children: [ @@ -241,90 +240,70 @@ class AudioPlayerState extends State { }, ), ), - Expanded( - child: Stack( - children: [ - if (waveform != null) - Container( - height: 40, - padding: const EdgeInsets.symmetric(horizontal: 24.0), - child: Row( - children: [ - for (var i = 0; i < waveform.length; i++) - Expanded( - child: Center( - child: Container( - decoration: BoxDecoration( - color: widget.color.withAlpha(64), - borderRadius: BorderRadius.circular(2), - ), - height: 56 * (waveform[i] / 1024), - ), - ), - ), - ], + const SizedBox(width: 8), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + for (var i = 0; i < AudioPlayerWidget.wavesCount; i++) + GestureDetector( + onTapDown: (_) => audioPlayer?.seek( + Duration( + milliseconds: + (maxPosition / AudioPlayerWidget.wavesCount).round() * + i, ), ), - if (audioPlayer != null) - SizedBox( - height: 40, - child: Slider( - activeColor: widget.color, - thumbColor: widget.color, - value: currentPosition, - min: 0, - max: maxPosition, - onChangeStart: (_) => audioPlayer.pause(), - onChangeEnd: (_) => audioPlayer.play(), - onChanged: (pos) => audioPlayer.seek( - Duration( - milliseconds: pos.round(), + child: Container( + height: 32, + color: widget.color.withAlpha(0), + alignment: Alignment.center, + child: Opacity( + opacity: currentPosition > i ? 1 : 0.5, + child: Container( + margin: const EdgeInsets.symmetric(horizontal: 1), + decoration: BoxDecoration( + color: widget.color, + borderRadius: BorderRadius.circular(2), ), + width: 2, + height: 32 * (waveform[i] / 1024), ), ), ), - ], - ), + ), + ], ), - Container( - alignment: Alignment.centerRight, - width: 42, + const SizedBox(width: 8), + SizedBox( + width: 36, child: Text( statusText, style: TextStyle( color: widget.color, + fontSize: 12, ), ), ), - const SizedBox(width: 4), - Stack( - children: [ - SizedBox( - width: buttonSize, - height: buttonSize, - child: InkWell( - splashColor: widget.color.withAlpha(128), - borderRadius: BorderRadius.circular(64), - onTap: audioPlayer == null ? null : _toggleSpeed, - child: Icon(Icons.mic_none_outlined, color: widget.color), - ), - ), - if (audioPlayer != null) - Positioned( - bottom: 0, - left: 0, - right: 0, - child: Text( + const SizedBox(width: 8), + Badge( + label: audioPlayer == null + ? null + : Text( '${audioPlayer.speed.toString()}x', - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 9.0, - color: widget.color, - ), ), - ), - ], + backgroundColor: Theme.of(context).colorScheme.secondary, + textColor: Theme.of(context).colorScheme.onSecondary, + child: InkWell( + splashColor: widget.color.withAlpha(128), + borderRadius: BorderRadius.circular(64), + onTap: audioPlayer == null ? null : _toggleSpeed, + child: Icon( + Icons.mic_none_outlined, + color: widget.color, + ), + ), ), + const SizedBox(width: 8), ], ), );