My goal is to print on my screen the name of the current playing song of my 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: [],
);
For this, I created a model "Sound" that contains all the informations I need such as :
class Sound {
final String id;
final String name;
final String url;
final String category;
Sound({
this.id = '',
required this.name,
required this.url,
required this.category,
});
static List<Sound> sounds = [
Sound(
id: '1',
name: 'Brume Long Norm',
url:
'https://myurl.mp3',
category: 'focus'), .... etc
Then, I filled the playlist's 'childrens' with a for loop in my initState() :
final tracks = Sound.sounds.toList();
tracks.forEach((element) => {
playlist.add(
AudioSource.uri(Uri.parse(element.url)),
)
So I would like to compare the audioUrl of the song that is currently playing, so I can try to match this url with one of my Sound object (Example : get sound.name where sound.url == audioUrlthatIsPlaying)
So for this, I guess I need to get the audioUrl of the current playing song.
I got this line in my initState(), I tried it everywhere else, it still logs me the same weird thing : I/flutter (28131): Instance of 'ProgressiveAudioSource'
player.durationStream.listen((duration) {
print(player.sequenceState?.currentSource );
});
Any idea of how I can get the URL string? or maybe there's another easy way to get the correct song name displayed?
You can store custom data in the tag
attribute of each audio source:
for (var sound in Sound.sounds) {
playlist.add(AudioSource.uri(Uri.parse(sound.url), tag: sound);
}
Then:
player.sequenceStateStream
.map((state) => state?.currentSource)
.whereNotNull()
.distinct()
.listen((source) {
final sound = source.tag as Sound;
print(sound.url);
});