Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
the last seconds of some music did not update
  • Loading branch information
vellt committed Mar 27, 2022
1 parent e470126 commit d210975
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 91 deletions.
51 changes: 20 additions & 31 deletions lib/controllers/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ import 'package:music_player_fluttter/controllers/stream_controller.dart';
import 'package:music_player_fluttter/models/stream.dart';

class PlayerController extends GetxController {
final Rx<AudioPlayer> _advancedPlayer = AudioPlayer().obs;
Rx<Duration> _duration = Duration().obs;
Rx<Duration> _position = Duration().obs;
final AudioPlayer _advancedPlayer = AudioPlayer();

Rx<Duration> duration = Duration(seconds: 0).obs;
Rx<Duration> position = Duration(seconds: 0).obs;
final Rx<int> currentStreamIndex = 0.obs;
final Rx<bool> isPlay = false.obs;
final Rx<PlayerState> playState = PlayerState.STOPPED.obs;
var streams = <Stream>[].obs;

@override
void onInit() {
// TODO: implement onInit
super.onInit();

final streamController = Get.put(StreamController());
streams = streamController.streams;
_advancedPlayer.value.onDurationChanged.listen((d) => _duration.value = d);
_advancedPlayer.value.onAudioPositionChanged
.listen((p) => _position.value = p);
_advancedPlayer.value.onPlayerStateChanged.listen((PlayerState event) {
isPlay.value = (event == PlayerState.PLAYING) ? true : false;
});

_advancedPlayer.onDurationChanged.listen((d) => duration.value = d);
_advancedPlayer.onAudioPositionChanged.listen((p) => position.value = p);
_advancedPlayer.onPlayerStateChanged
.listen((PlayerState state) => playState.value = state);
_advancedPlayer.onPlayerCompletion
.listen((event) => position.value = duration.value);
}

//play
void smartPlay() async {
if (isPlay.value) {
if (playState.value == PlayerState.PLAYING) {
pause();
} else {
resume();
Expand All @@ -41,22 +44,22 @@ class PlayerController extends GetxController {

//play
void resume() async {
int result = await _advancedPlayer.value
.play(streams[currentStreamIndex.value].music!);
int result =
await _advancedPlayer.play(streams[currentStreamIndex.value].music!);
if (result == 1) ; //success
}

//pause
void pause() async {
int result = await _advancedPlayer.value.pause();
int result = await _advancedPlayer.pause();
if (result == 1) ; //success
}

//stop
void stop() async {
int result = await _advancedPlayer.value.stop();
int result = await _advancedPlayer.stop();
if (result == 1) {
_position.value = Duration(seconds: 0);
position.value = Duration(seconds: 0);
}
}

Expand All @@ -71,20 +74,6 @@ class PlayerController extends GetxController {
play();
}

String _format(Duration d) {
String minute =
int.parse(d.toString().split('.').first.padLeft(8, "0").split(':')[1])
.toString();
String second = d.toString().split('.').first.padLeft(8, "0").split(':')[2];
return ("$minute:$second");
}

set setPositionValue(double value) =>
_advancedPlayer.value.seek(Duration(seconds: value.toInt()));
set setCurrentStreamIndex(index) => currentStreamIndex.value = index;
int get getCurrentStreamIndex => currentStreamIndex.value;
double get getDurationAsDouble => _duration.value.inSeconds.toDouble();
String get getDurationAsFormatSting => _format(_duration.value);
double get getPositionAsDouble => _position.value.inSeconds.toDouble();
String get getPositionAsFormatSting => _format(_position.value);
_advancedPlayer.seek(Duration(seconds: value.toInt()));
}
10 changes: 10 additions & 0 deletions lib/extensions/duration_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extension durationExtensions on Duration {
String get timeFormat {
String minute = int.parse(
this.toString().split('.').first.padLeft(8, "0").split(':')[1])
.toString();
String second =
this.toString().split('.').first.padLeft(8, "0").split(':')[2];
return ("$minute:$second");
}
}
128 changes: 68 additions & 60 deletions lib/views/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:music_player_fluttter/controllers/player_controller.dart';
import 'package:sizer/sizer.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:music_player_fluttter/extensions/duration_extensions.dart';

class HomePage extends StatelessWidget {
HomePage({Key? key}) : super(key: key);
Expand Down Expand Up @@ -56,9 +58,9 @@ class HomePage extends StatelessWidget {
],
),
),
Expanded(
child: Obx(() {
return ListView.builder(
Flexible(
child: Obx(
() => ListView.builder(
itemCount: playerController.streams.length,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
Expand All @@ -69,18 +71,18 @@ class HomePage extends StatelessWidget {
Radius.circular(10.sp),
),
onTap: () {
playerController.setCurrentStreamIndex = index;
playerController.currentStreamIndex.value = index;
playerController.play();
},
child: Obx(
() => Container(
height: 52.sp,
decoration: BoxDecoration(
color:
(playerController.getCurrentStreamIndex ==
index)
? Color(0xFF2A2A2A)
: Colors.transparent,
color: (playerController
.currentStreamIndex.value ==
index)
? Color(0xFF2A2A2A)
: Colors.transparent,
borderRadius: BorderRadius.all(
Radius.circular(10.sp),
),
Expand Down Expand Up @@ -186,8 +188,8 @@ class HomePage extends StatelessWidget {
),
);
},
);
}),
),
),
),
Container(
decoration: BoxDecoration(
Expand All @@ -200,15 +202,15 @@ class HomePage extends StatelessWidget {
),
],
),
child: Column(
children: [
Padding(
padding: EdgeInsets.only(right: 15.sp, left: 15.sp),
child: GetX<PlayerController>(builder: (controller) {
return Row(
child: Obx(
() => Column(
children: [
Padding(
padding: EdgeInsets.only(right: 15.sp, left: 15.sp),
child: Row(
children: [
Text(
controller.getPositionAsFormatSting,
playerController.position.value.timeFormat,
style: TextStyle(
fontSize: 15.sp,
color: Colors.white,
Expand All @@ -218,69 +220,75 @@ class HomePage extends StatelessWidget {
child: Slider(
activeColor: Color(0xFF71B77A),
inactiveColor: Color(0xFFEFEFEF),
value: controller.getPositionAsDouble,
value: playerController
.position.value.inSeconds
.toDouble(),
min: 0.0,
max: controller.getDurationAsDouble,
max: playerController.duration.value.inSeconds
.toDouble() +
1.0,
onChanged: (double value) {
controller.setPositionValue = value;
playerController.setPositionValue = value;
}),
),
Obx(
() => Text(
controller.getDurationAsFormatSting,
style: TextStyle(
fontSize: 15.sp,
color: Colors.white,
),
Text(
playerController.duration.value.timeFormat,
style: TextStyle(
fontSize: 15.sp,
color: Colors.white,
),
),
],
);
}),
),
Padding(
padding: EdgeInsets.only(
right: 10.sp, left: 10.sp, top: 5.sp, bottom: 16.sp),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
),
),
Padding(
padding: EdgeInsets.only(
right: 10.sp,
left: 10.sp,
top: 5.sp,
bottom: 16.sp),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {
playerController.back();
},
iconSize: 35.sp,
icon: Icon(
Icons.skip_previous,
color: Colors.white,
)),
IconButton(
),
),
IconButton(
onPressed: () {
playerController.smartPlay();
},
iconSize: 60.sp,
icon: Obx(
() => Icon(
(playerController.isPlay.value)
? Icons.pause
: Icons.play_arrow,
color: Colors.white,
),
)),
IconButton(
onPressed: () {
playerController.next();
},
iconSize: 35.sp,
icon: Icon(
Icons.skip_next,
(playerController.playState.value ==
PlayerState.PLAYING)
? Icons.pause
: Icons.play_arrow,
color: Colors.white,
)),
],
),
)
],
),
),
IconButton(
onPressed: () {
playerController.next();
},
iconSize: 35.sp,
icon: Icon(
Icons.skip_next,
color: Colors.white,
)),
],
),
)
],
),
),
)
),
],
),
),
Expand Down

0 comments on commit d210975

Please sign in to comment.