Enhance audio player functionality by auto-playing on load and resetting position at end
This commit is contained in:
@@ -54,6 +54,7 @@ class _AudioPlayerBarState extends State<AudioPlayerBar>
|
|||||||
_duration = _audioPlayer.duration ?? Duration.zero;
|
_duration = _audioPlayer.duration ?? Duration.zero;
|
||||||
_isLoading = false;
|
_isLoading = false;
|
||||||
});
|
});
|
||||||
|
_audioPlayer.play(); // Start playback automatically
|
||||||
_audioPlayer.positionStream.listen((pos) {
|
_audioPlayer.positionStream.listen((pos) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_position = pos;
|
_position = pos;
|
||||||
@@ -76,6 +77,10 @@ class _AudioPlayerBarState extends State<AudioPlayerBar>
|
|||||||
_audioPlayer.pause();
|
_audioPlayer.pause();
|
||||||
_iconController.reverse();
|
_iconController.reverse();
|
||||||
} else {
|
} else {
|
||||||
|
// If at end, seek to start
|
||||||
|
if (_position >= _duration && _duration > Duration.zero) {
|
||||||
|
_audioPlayer.seek(Duration.zero);
|
||||||
|
}
|
||||||
_audioPlayer.play();
|
_audioPlayer.play();
|
||||||
_iconController.forward();
|
_iconController.forward();
|
||||||
}
|
}
|
||||||
@@ -83,16 +88,6 @@ class _AudioPlayerBarState extends State<AudioPlayerBar>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// Sync icon animation with state
|
|
||||||
if (_isPlaying &&
|
|
||||||
_iconController.status != AnimationStatus.forward &&
|
|
||||||
_iconController.value == 0.0) {
|
|
||||||
_iconController.forward();
|
|
||||||
} else if (!_isPlaying &&
|
|
||||||
_iconController.status != AnimationStatus.reverse &&
|
|
||||||
_iconController.value == 1.0) {
|
|
||||||
_iconController.reverse();
|
|
||||||
}
|
|
||||||
return AnimatedContainer(
|
return AnimatedContainer(
|
||||||
duration: const Duration(milliseconds: 300),
|
duration: const Duration(milliseconds: 300),
|
||||||
height: 48,
|
height: 48,
|
||||||
|
|||||||
Reference in New Issue
Block a user