【发布时间】:2022-08-17 01:02:15
【问题描述】:
我正在尝试一次播放一首音乐,但当前发生的是多个音频文件同时播放。基本上有两个屏幕 - 一个用于列出所有音乐(在 ListView 构建器内),第二个用于播放(这是播放音乐的地方)
当我尝试播放任何其他音乐时(当一首正在播放时),前一首不会停止,它们会同时播放。所以我试图实现的是,当我播放任何其他音乐时,以前的音乐应该自动停止
// This button is inside the list view builder, when user click on it, it would redirect to Music Player screen where the music is being played
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.purpleAccent
),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => MusicPlayerScreen(songInfo: snapshot.data![index])));
},
child: Text(\'PLAY\', style: GoogleFonts.oswald(color: Colors.white))
),
// Music Screen
class MusicPlayerScreen extends StatefulWidget {
final SongInfo songInfo;
const MusicPlayerScreen({Key? key, required this.songInfo}) : super(key: key);
@override
State<MusicPlayerScreen> createState() => _MusicPlayerScreenState();
}
class _MusicPlayerScreenState extends State<MusicPlayerScreen> {
final AudioPlayer audioPlayer = AudioPlayer();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xff313254),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(left: 30.0, right: 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
InkWell(
onTap: () async {
await audioPlayer.play(UrlSource(widget.songInfo.filePath));
},
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: const LinearGradient(colors: [
Colors.purpleAccent,
Colors.purple
]),
boxShadow: [
BoxShadow(
color: Colors.purpleAccent.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: const Offset(0, 3)
),
],
),
child: Center(child: Icon(Icons.play_circle_fill, color: Colors.white, size: 35.0)),
),
),
],
),
)
],
),
),
),
);
}
}
标签: flutter dart audio-player