简体   繁体   English

当另一首歌曲在 Android 中开始播放时,我如何停止播放歌曲

[英]How can i stop playing a song when another song start playing in Android

Hello everyone ia listview with items in it.大家好,列表视图中有项目。 The moment you click on a item you hear a song but if you click on another item the first song doesn't stop it plays two songs at the same time how can i stop the first song if i click the second song or stop the second one if i click the first song当您单击一个项目时,您会听到一首歌,但是如果您单击另一个项目,第一首歌曲不会停止它同时播放两首歌曲,如果我单击第二首歌曲或停止第二首歌曲,我如何停止第一首歌曲如果我点击第一首歌

my code我的代码

private MediaPlayer mp;

  final ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomListAdapter(this, image_details));


        // When the user clicks on the ListItem
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Object o = listView.getItemAtPosition(position);
                final Songs titel = (titel) o;



                // get the club song from the arraylist
                mp = MediaPlayer.create(a.getContext(), titel.getClubLied());
                mp.start();

               mp.stop();
        });






 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                mp.stop();
                return  true;
            }
        });

How can i fix this i used start and stop so the moment other song starts it stops but this doesnt work我怎样才能解决这个问题,我使用了开始和停止,所以当其他歌曲开始时它会停止,但这不起作用

you can check if MediaPlayer is playing then stop or release it first then play new song like that您可以检查 MediaPlayer 是否正在播放,然后先停止或释放它,然后像这样播放新歌

           if (mp.isPlaying()) {
                 mp.stop();
                 mp.reset();
                 mp.release();
            }
            mp = MediaPlayer.create(a.getContext(), titel.getClubLied());
            mp.start();

Hope its work;希望它的工作;

您可以将MediaPlayer声明为静态,以便一次仅运行 1 个项目。

You have to release your mediaplayer to stop the mediaplayer like below:你必须release你的媒体播放器来停止媒体播放器,如下所示:

@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {

            Object o = listView.getItemAtPosition(position);
            final Songs titel = (titel) o;

            if (mp != null) {
                mp.stop();
                mp.reset();
                mp.release();
            }

            mp = MediaPlayer.create(a.getContext(), titel.getClubLied());
            mp.start();
    });

You are creating mediaplayer in on setOnItemClickListener so whenever you click item it creates new mediaplayer so previous one is play continue so create media player outside the setOnItemClickListener than pass only song data..您正在 setOnItemClickListener 上创建媒体播放器,因此每当您单击项目时,它都会创建新的媒体播放器,因此前一个播放器会继续播放,因此在 setOnItemClickListener 之外创建媒体播放器,而不是仅传递歌曲数据。

private MediaPlayer mp;

  final ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomListAdapter(this, image_details));

mp = new MediaPlayer();
        // When the user clicks on the ListItem
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Object o = listView.getItemAtPosition(position);
                final Songs titel = (titel) o;



                // get the club song from the arraylist
        mp.reset()
        mp.setDataSource(a.getContext(), uri);
        mp.prepare();
        mp.start();


        });






 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                mp.stop();
                return  true;
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM