Skip to content

Commit

Permalink
feat: transform isInitialized into an observable in DSAudioPlayerCont…
Browse files Browse the repository at this point in the history
…roller
  • Loading branch information
Marcelo Amaro committed Oct 13, 2023
1 parent 8ccde5e commit f171264
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
3 changes: 1 addition & 2 deletions lib/src/controllers/chat/ds_audio_player.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import 'package:rxdart/rxdart.dart' as rx_dart;
class DSAudioPlayerController extends GetxController {
final audioSpeed = RxDouble(1.0);
final player = AudioPlayer();

bool isInitialized = false;
final isInitialized = RxBool(false);

/// Collects the data useful for displaying in a SeekBar widget.
///
Expand Down
55 changes: 31 additions & 24 deletions lib/src/widgets/chat/audio/ds_audio_player.widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class _DSAudioPlayerState extends State<DSAudioPlayer>
),
);

_controller.isInitialized = true;
_controller.isInitialized.value = true;
} catch (_) {
_controller.isInitialized = false;
_controller.isInitialized.value = false;
}
}

Expand Down Expand Up @@ -176,15 +176,17 @@ class _DSAudioPlayerState extends State<DSAudioPlayer>
final processingState = playerState?.processingState;
final playing = playerState?.playing;
if (playing != true) {
return DSPlayButton(
onPressed: _controller.isInitialized
? _controller.player.play
: () => {},
isLoading: [ProcessingState.loading, ProcessingState.buffering]
.contains(processingState),
color: _controller.isInitialized
? widget.controlForegroundColor
: DSColors.contentDisable,
return Obx(
() => DSPlayButton(
onPressed: _controller.isInitialized.value
? _controller.player.play
: () => {},
isLoading: [ProcessingState.loading, ProcessingState.buffering]
.contains(processingState),
color: _controller.isInitialized.value
? widget.controlForegroundColor
: DSColors.contentDisable,
),
);
} else if (processingState != ProcessingState.completed) {
return DSPauseButton(
Expand All @@ -202,19 +204,24 @@ class _DSAudioPlayerState extends State<DSAudioPlayer>
stream: _controller.positionDataStream,
builder: (context, snapshot) {
final positionData = snapshot.data;
return DSAudioSeekBar(
duration: positionData?.duration ?? Duration.zero,
position: positionData?.position ?? Duration.zero,
bufferedPosition: positionData?.bufferedPosition ?? Duration.zero,
onChangeEnd:
_controller.isInitialized ? _controller.player.play : null,
onChanged: _controller.isInitialized ? _controller.player.seek : null,
onChangeStart: _controller.player.pause,
labelColor: widget.labelColor,
bufferActiveTrackColor: widget.bufferActiveTrackColor,
bufferInactiveTrackColor: widget.bufferInactiveTrackColor,
sliderActiveTrackColor: widget.sliderActiveTrackColor,
sliderThumbColor: widget.sliderThumbColor,
return Obx(
() => DSAudioSeekBar(
duration: positionData?.duration ?? Duration.zero,
position: positionData?.position ?? Duration.zero,
bufferedPosition: positionData?.bufferedPosition ?? Duration.zero,
onChangeEnd: _controller.isInitialized.value
? _controller.player.play
: null,
onChanged: _controller.isInitialized.value
? _controller.player.seek
: null,
onChangeStart: _controller.player.pause,
labelColor: widget.labelColor,
bufferActiveTrackColor: widget.bufferActiveTrackColor,
bufferInactiveTrackColor: widget.bufferInactiveTrackColor,
sliderActiveTrackColor: widget.sliderActiveTrackColor,
sliderThumbColor: widget.sliderThumbColor,
),
);
},
);
Expand Down

0 comments on commit f171264

Please sign in to comment.