Skip to content

Commit

Permalink
chore: Display normal Slider when no waveform provided in audioplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Dec 3, 2024
1 parent 5321a37 commit cd611ae
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions lib/pages/chat/events/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
return '${duration.inMinutes.toString().padLeft(2, '0')}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
}

List<int> _getWaveform() {
List<int>? _getWaveform() {
final eventWaveForm = widget.event.content
.tryGetMap<String, dynamic>('org.matrix.msc1767.audio')
?.tryGetList<int>('waveform');
if (eventWaveForm == null || eventWaveForm.isEmpty) {
return List<int>.filled(AudioPlayerWidget.wavesCount, 500);
return null;
}
while (eventWaveForm.length < AudioPlayerWidget.wavesCount) {
for (var i = 0; i < eventWaveForm.length; i = i + 2) {
Expand All @@ -200,7 +200,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
return eventWaveForm.map((i) => i > 1024 ? 1024 : i).toList();
}

late final List<int> waveform;
late final List<int>? _waveform;

void _toggleSpeed() async {
final audioPlayer = this.audioPlayer;
Expand Down Expand Up @@ -229,12 +229,13 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
@override
void initState() {
super.initState();
waveform = _getWaveform();
_waveform = _getWaveform();
}

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final waveform = _waveform;

final statusText = this.statusText ??= _durationString ?? '00:00';
final audioPlayer = this.audioPlayer;
Expand Down Expand Up @@ -290,43 +291,48 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
Expanded(
child: Stack(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
for (var i = 0;
i < AudioPlayerWidget.wavesCount;
i++)
Expanded(
child: Container(
height: 32,
alignment: Alignment.center,
if (waveform != null)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
for (var i = 0;
i < AudioPlayerWidget.wavesCount;
i++)
Expanded(
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 1,
height: 32,
alignment: Alignment.center,
child: Container(
margin: const EdgeInsets.symmetric(
horizontal: 1,
),
decoration: BoxDecoration(
color: i < wavePosition
? widget.color
: widget.color.withAlpha(128),
borderRadius: BorderRadius.circular(64),
),
height: 32 * (waveform[i] / 1024),
),
decoration: BoxDecoration(
color: i < wavePosition
? widget.color
: widget.color.withAlpha(128),
borderRadius: BorderRadius.circular(64),
),
height: 32 * (waveform[i] / 1024),
),
),
),
],
],
),
),
),
SizedBox(
height: 32,
child: Slider(
thumbColor: widget.event.senderId ==
widget.event.room.client.userID
? theme.colorScheme.onPrimary
: theme.colorScheme.primary,
activeColor: Colors.transparent,
inactiveColor: Colors.transparent,
activeColor: waveform == null
? widget.color
: Colors.transparent,
inactiveColor: waveform == null
? widget.color.withAlpha(128)
: Colors.transparent,
max: maxPosition,
value: currentPosition,
onChanged: (position) => audioPlayer == null
Expand Down

0 comments on commit cd611ae

Please sign in to comment.