Skip to content

Commit

Permalink
change music time dragging slider
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus de Morais Ramalho committed Jul 5, 2021
1 parent 30531e7 commit d202503
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RxDartMusicPlayerPresenter implements MusicPlayerPresenter {
_audioPlayer!.positionStream;

@override
void initState(String musicPath) {
void initState(String musicPath) async {
_audioPlayer = AudioPlayer();

_audioPlayer!.setAsset(musicPath);
Expand Down Expand Up @@ -70,4 +70,12 @@ class RxDartMusicPlayerPresenter implements MusicPlayerPresenter {
Future<void> onVolumeChanged(double newVolume) async {
await _audioPlayer!.setVolume(newVolume);
}

@override
Future<void> changeMusicTime(int seconds) async {
_audioPlayer!.seek(
Duration(seconds: seconds),
index: _audioPlayer!.effectiveIndices!.first,
);
}
}
49 changes: 26 additions & 23 deletions lib/ui/pages/music_player/music_player_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
topRight: Radius.circular(24),
),
child: Material(
color: Colors.transparent,
color: values.KColors.blue,
child: Container(
height: double.maxFinite,
width: double.maxFinite,
Expand Down Expand Up @@ -111,9 +111,9 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
bottomRight: Radius.circular(24),
),
child: Material(
color: values.KColors.blue,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16),
color: values.KColors.blue,
child: Column(
children: [
const VerticalSpacing(27),
Expand Down Expand Up @@ -152,20 +152,29 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
return Column(
children: [
StreamBuilder<Duration>(
stream: presenter.currentPositionAtMusicStream,
initialData: Duration.zero,
builder: (context, snapshot) {
final currentPositionInSeconds =
snapshot.data!.inSeconds.toDouble();
stream: presenter.currentPositionAtMusicStream,
initialData: Duration.zero,
builder: (context, snapshot) {
var actualMusicPositionInSeconds =
snapshot.data!.inSeconds.toDouble();

return Slider(
value: currentPositionInSeconds,
max: presenter.musicDurationInSeconds.toDouble(),
onChanged: (_) {},
inactiveColor: values.KColors.white.withOpacity(0.38),
activeColor: values.KColors.white,
);
}),
final musicDurationInSeconds = presenter.musicDurationInSeconds;

if (actualMusicPositionInSeconds > musicDurationInSeconds) {
actualMusicPositionInSeconds = musicDurationInSeconds;
}

return Slider(
value: actualMusicPositionInSeconds,
max: musicDurationInSeconds,
onChanged: (musicTime) async => presenter.changeMusicTime(
musicTime.toInt(),
),
inactiveColor: values.KColors.white.withOpacity(0.38),
activeColor: values.KColors.white,
);
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand Down Expand Up @@ -225,12 +234,6 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
builder: (context, snapshot) {
final playerState = snapshot.data ?? MusicPlayerState.initial();

if (playerState.status.isInitializing) {
return Center(
child: CircularProgressIndicator(),
);
}

if (!playerState.playing) {
return IconButton(
padding: EdgeInsets.zero,
Expand Down Expand Up @@ -292,9 +295,9 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
initialData: 1.0,
builder: (context, snapshot) {
final volume = snapshot.data!;
print(volume);

return Slider(
max: 2,
max: 1,
value: volume,
onChanged: presenter.onVolumeChanged,
inactiveColor: values.KColors.white.withOpacity(0.38),
Expand Down
1 change: 1 addition & 0 deletions lib/ui/pages/music_player/music_player_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ abstract class MusicPlayerPresenter {
Future<void> pause();
Future<void> replay();
Future<void> onVolumeChanged(double newVolume);
Future<void> changeMusicTime(int seconds);
}

0 comments on commit d202503

Please sign in to comment.