【问题标题】:Audio's are playing simultaneously issue - Flutter AudioPlayers音频正在同时播放问题 - Flutter AudioPlayers
【发布时间】: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


    【解决方案1】:

    当您弹出此屏幕时,您必须处理播放器

     @override
      void dispose() {
       audioPlayer.dispose();
        super.dispose();
      }
    

    将此添加到 MusicPlayerScreen。如果您希望仅在播放新音乐时停止它,您可能必须为所有屏幕使用通用播放器并将其传递给 musicPlayerScreen 并在该播放器本身上播放音乐,以免重复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多