just_audio
package for audio playback, dart:math
for generating random numbers, and dart:async
for handling asynchronous operations.import 'package:just_audio/just_audio.dart';
import 'dart:math';
import 'dart:async';
class AdvanceMusicPlayer extends StatefulWidget {
const AdvanceMusicPlayer({
Key? key,
this.width,
this.height,
required this.initialUrl,
required this.musicUrls,
required this.sliderActiveColor,
required this.sliderInactiveColor,
required this.backwardIconPath,
required this.forwardIconPath,
required this.backwardIconColor,
required this.forwardIconColor,
required this.pauseIconPath,
required this.playIconPath,
required this.pauseIconColor,
required this.playIconColor,
required this.loopIconPath,
required this.loopIconColor,
required this.shuffleIconPath,
required this.shuffleIconColor,
required this.playbackDurationTextColor,
required this.previousIconPath,
required this.nextIconPath,
required this.previousIconColor,
required this.nextIconColor,
required this.loopIconPressedPath,
required this.shuffleIconPressedPath,
required this.speakerOnIconPath,
required this.speakerOffIconPath,
required this.speakerOnIconColor,
required this.speakerOffIconColor,
required this.dropdownTextColor,
required this.timerIcon,
}) : super(key: key);
// Widget properties
final double? width;
final double? height;
final String initialUrl;
final List<String> musicUrls;
final Color sliderActiveColor;
final Color sliderInactiveColor;
final Widget backwardIconPath;
final Widget forwardIconPath;
final Color backwardIconColor;
final Color forwardIconColor;
final Widget pauseIconPath;
final Widget playIconPath;
final Color pauseIconColor;
final Color playIconColor;
final Widget loopIconPath;
final Color loopIconColor;
final Widget shuffleIconPath;
final Color shuffleIconColor;
final Color playbackDurationTextColor;
final Widget previousIconPath;
final Widget nextIconPath;
final Color previousIconColor;
final Color nextIconColor;
final Widget loopIconPressedPath;
final Widget shuffleIconPressedPath;
final Widget speakerOnIconPath;
final Widget speakerOffIconPath;
final Color speakerOnIconColor;
final Color speakerOffIconColor;
final Color dropdownTextColor;
final Widget timerIcon;
@override
_AdvanceMusicPlayerState createState() => _AdvanceMusicPlayerState();
}
This code defines a StatefulWidget called AdvanceMusicPlayer
. It represents the advanced music player widget and accepts several properties for customization. These properties include dimensions (width
and height
), URLs of music tracks (initialUrl
and musicUrls
), colors for the slider (sliderActiveColor
and sliderInactiveColor
), icons for different controls (play, pause, backward, forward, etc.), and various other customization options.
class _AdvanceMusicPlayerState extends State<AdvanceMusicPlayer> with SingleTickerProviderStateMixin {
late AudioPlayer audioPlayer;
bool isPlaying = false;
Duration totalDuration = Duration.zero;
Duration currentPosition = Duration.zero;
int currentSongIndex = 0;
bool isLooping = false;
bool isShuffling = false;
bool isSpeakerOn = true;
String playbackSpeed = 'Normal';
Map<String, double> speedValues = {
'0.25x': 0.25,
'0.5x': 0.5,
'0.75x': 0.75,
'Normal': 1.0,
'1.25x': 1.25,
'1.5x': 1.5,
'1.75': 1.75,
'2x': 2.0,
};
late AnimationController _animationController;
late Animation<double> _fadeAnimation;
Duration? selectedTimer;
final List<Duration> timerOptions = [
Duration(minutes: 1),
Duration(minutes: 10),
Duration(minutes: 15),
Duration(minutes: 20),
Duration(minutes: 25),
Duration(minutes: 30),
Duration(minutes: 35),
Duration(minutes: 40),
Duration(minutes: 45),
Duration(minutes: 50),
Duration(minutes: 55),
Duration(minutes: 60),
Duration(minutes: 90),
Duration(minutes: 120),
];
Timer? countdownTimer;
The _AdvanceMusicPlayerState
class represents the state of the AdvanceMusicPlayer
widget. It extends the State
class and uses the SingleTickerProviderStateMixin
to handle animations.
The class defines several variables for managing the state of the music player. Here's a brief explanation of each variable:
audioPlayer
: An instance of the AudioPlayer
class from the just_audio
package for playing audio.isPlaying
: A boolean indicating whether the music is currently playing.totalDuration
: The total duration of the currently playing audio track.currentPosition
: The current position within the audio track.currentSongIndex
: The index of the current song in the musicUrls
list.isLooping
: A boolean indicating whether looping is enabled.isShuffling
: A boolean indicating whether shuffling is enabled.isSpeakerOn
: A boolean indicating whether the speaker is turned on.playbackSpeed
: A string representing the current playback speed.speedValues
: A map that maps playback speed labels to their corresponding speed values._animationController
: An animation controller for managing animations._fadeAnimation
: An animation for fading in the widget.