flutter: just_audio player plugin seekToNext and seekToPrevi



我新的扑动和我创建一个音乐播放器使用just_audio插件,但我有困难实现的下一个按钮,我想改变当前播放的歌曲。

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,
));
}),

重要的是要注意,这里您正在使用音频剪辑,这意味着您正在使用单个歌曲。然而,播放列表是just_audio中提供的另一个选项。包。

使用无间隙播放列表。

`// 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);`

当你和孩子们打交道时,你必须把音频列表传递给他们。在之后,您可以使用索引来确定将播放哪首歌曲当您选择歌曲时,您可以选择播放之前的或其他选项

相关内容

最新更新