Im new to flutter and Im creating a music player using just_audio plugin but I having trouble implementing the nxt button where I want to change the song that currently playing.
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: item.data!.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(item.data![index].title),
trailing: IconButton(
icon: const Icon(Icons.play_arrow),
onPressed: () {
playmusichandler('${item.data![index].uri}');
},
),
t
void playmusic() async{
try {
await widget.audioPlayer
.setAudioSource(AudioSource.uri(Uri.parse(widget.songModel.uri!)));
await widget.audioPlayer.play();
} catch (e) {
debugPrint('$e');
}
}
==
StreamBuilder<SequenceState?>(
stream: widget.audioPlayer.sequenceStateStream,
builder: (context, index) {
return IconButton(
onPressed: () {
widget.audioPlayer.hasNext
? widget.audioPlayer.seekToNext()
: null;
},
icon: const Icon(
Icons.skip_next,
size: 45.0,
color: Colors.white,
));
}),
CodePudding user response:
It is important to note that here you are working with audio clips which means you are using single songs. Nevertheless, a playlist is another option available in the just_audio package.
Working with gapless playlists.
`// Define the playlist
final playlist = ConcatenatingAudioSource(
// Start loading next item just before reaching it
useLazyPreparation: true,
// Customise the shuffle algorithm
shuffleOrder: DefaultShuffleOrder(),
// Specify the playlist items
children: [
AudioSource.uri(Uri.parse('https://example.com/track1.mp3')),
AudioSource.uri(Uri.parse('https://example.com/track2.mp3')),
AudioSource.uri(Uri.parse('https://example.com/track3.mp3')),
],
);
// Load and play the playlist
await player.setAudioSource(playlist, initialIndex: 0, initialPosition: Duration.zero);
await player.seekToNext(); // Skip to the next item
await player.seekToPrevious(); // Skip to the previous item
await player.seek(Duration.zero, index: 2); // Skip to the start of track3.mp3
await player.setLoopMode(LoopMode.all); // Set playlist to loop (off|all|one)
await player.setShuffleModeEnabled(true); // Shuffle playlist order (true|false)
// Update the playlist
await playlist.add(newChild1);
await playlist.insert(3, newChild2);
await playlist.removeAt(3);`
When you are dealing with children, you have to pass the audio list to them. Following that, you can use the index to determine which song will play when you select a song, then you can choose to play previous or other options