我一直在尝试将assets_audio_player
用于播放列表。我想从 firestore 集合中动态获取数据,但我无法将所有歌曲都放入播放列表。
我一直在尝试使用来自 Firestore 的数据动态填充音频列表,但没有成功。
我在 db 收藏中有 3 首歌曲,但我只能将其中 1 首加入音频列表。
我不想像插件示例中所示静态添加歌曲网址。有人可以帮忙吗?
这是我的尝试:
首先,我使用 StreamBuilder 获取数据并将其传递给AudioPlayerBackgroundPlaylist();
children:
snapShot.data.docs.map((DocumentSnapshot document) {
return InkWell(
onTap: () {
MaterialPageRoute(builder: (
context) => AudioPlayerBackgroundPlaylist (
song: document.data()['song'],
songTitle: document.data()['songTitle'],
artistName: document.data()['artist']['artistName'],
)));
这是AudioPlayerBackgroundPlaylist
我希望看到歌曲的页面,但我只看到 db 集合中的三首歌曲中的一首
class AudioPlayerBackgroundPlaylist extends StatefulWidget {
String songTitle, artistName, song;
AudioPlayerBackgroundPlaylist(
{this.songTitle,
this.artistName,
this.song,
});
@override
_AudioPlayerBackgroundPlaylistState createState() =>
_AudioPlayerBackgroundPlaylistState();
}
class _AudioPlayerBackgroundPlaylistState
extends State<AudioPlayerBackgroundPlaylist> {
final AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
List<Audio> audios;
@override
void initState() {
audios = [
Audio.network(
widget.song,
metas: Metas(title: widget.songTitle, artist: widget.artistName)),
]
super.initState();
setupPlaylist();
}
@override
void dispose() {
super.dispose();
audioPlayer.dispose();
}
void setupPlaylist() async {
audioPlayer.open(
Playlist(audios: audios),
showNotification: true,
autoStart: false);
}
playMusic() async {
await audioPlayer.play();
}
pauseMusic() async {
await audioPlayer.pause();
}
skipPrevious() async {
await audioPlayer.previous();
}
skipNext() async {
await audioPlayer.next();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.center,
child: audioPlayer.builderIsPlaying(builder: (context, isPlaying) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
iconSize: 50,
icon: Icon(Icons.skip_previous_rounded),
onPressed: () => skipPrevious()),
IconButton(
iconSize: 50,
icon: Icon(isPlaying
? Icons.pause_rounded
: Icons.play_arrow_rounded),
onPressed: () => isPlaying ? pauseMusic() : playMusic()),
IconButton(
iconSize: 50,
icon: Icon(Icons.skip_next_rounded),
onPressed: () => skipNext())
],
);
}),
),
);
}
}