当用户单击下一个播放按钮时,我想停止当前播放的音频

时间:2018-11-18 14:37:16

标签: java android

我的应用程序遇到问题。所有音频都播放良好,但问题是当我按第一个按钮开始播放音频时,它会播放它,如果我单击也开始播放的下一个播放按钮,但第一个音频不会停止。如何停止。请帮助

enter image description here 在这里,我的应用程序Java代码:

    public class ringtone_tab extends AppCompatActivity {

...

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ringtone_tab);


            clk6 = (Button) findViewById(R.id.btn_play6);

            clk5 = (Button) findViewById(R.id.btn_play5);

            clk4 = (Button) findViewById(R.id.btn_play4);

            clk3 = (Button) findViewById(R.id.btn_play3);

            clk2 = (Button) findViewById(R.id.btn_play2);

            clk1 = (Button) findViewById(R.id.btn_play1);



            mdx6 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_vandana);

            mdx5 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_tandav_mantra);

            mdx4 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_shiv_om);

            mdx3 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_shiv);

            mdx2 = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_aaradhna);

            mdx = MediaPlayer.create(ringtone_tab.this,R.raw.shiv_shankar);
        }


        public void setBtn_play6(View v)
        {



            if(mdx6.isPlaying())
            {
                mdx6.stop();
                mdx6.reset();
                mdx6.release();
            }
            mdx6 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
            mdx6.start();

        }

        public void setBtn_play5(View v)
        {



            if(mdx5.isPlaying())
            {
                mdx5.stop();
                mdx5.reset();
                mdx5.release();
            }
            mdx5 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
            mdx5.start();

        }



        public void setBtn_play4(View v)
        {



            if(mdx4.isPlaying())
            {
                mdx4.stop();
                mdx4.reset();
                mdx4.release();
            }
            mdx4 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
            mdx4.start();

        }

        public void setBtn_play3(View v)
        {



            if(mdx3.isPlaying())
            {
                mdx3.stop();
                mdx3.reset();
                mdx3.release();
            }
            mdx3 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
            mdx3.start();

        }



        public void setBtn_play2(View v)
        {



            if(mdx2.isPlaying())
            {
                mdx2.stop();
                mdx2.reset();
                mdx2.release();
            }
            mdx2 = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
            mdx2.start();


        }


        public void setBtn_play1(View v)
        {

            if(mdx.isPlaying())
            {
                mdx.stop();
                mdx.reset();
                mdx.release();
            }
            mdx = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
            mdx.start();
        }
        }

1 个答案:

答案 0 :(得分:0)

引入新的方法调用,例如stopAllplayers

private void stopAllPlayers(){

if(mdx1 != null && mdx1.isPlaying())
        {mdx1.stop();mdx1.reset(); mdx1.release();}
if(mdx2 != null && mdx2.isPlaying())
        {mdx2.stop();mdx2.reset(); mdx2.release();}
if(mdx3 != null && mdx3.isPlaying())
        {mdx3.stop();mdx3.reset(); mdx3.release();}
if(mdx4 != null && mdx4.isPlaying())
        {mdx4.stop();mdx4.reset(); mdx4.release();}
if(mdx5 != null && mdx5.isPlaying())
        {mdx5.stop();mdx5.reset(); mdx5.release();}
if(mdx6 != null && mdx6.isPlaying())
        {mdx6.stop();mdx6.reset(); mdx6.release();}
}

然后在所有播放方法中调用此方法。

public void setBtn_play6(View v)
    {
      stopAllPlayers()
      ........ 

对所有setBtn_play1,setBtn_play2 ........

在一个媒体播放器上使用以下代码

public class ringtone_tab extends AppCompatActivity {

    Button clk1;
    Button clk2;
    Button clk3;
    Button clk4;
    Button clk5;
    Button clk6;

    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringtone_tab);


        clk6 = (Button) findViewById(R.id.btn_play6);

        clk5 = (Button) findViewById(R.id.btn_play5);

        clk4 = (Button) findViewById(R.id.btn_play4);

        clk3 = (Button) findViewById(R.id.btn_play3);

        clk2 = (Button) findViewById(R.id.btn_play2);

        clk1 = (Button) findViewById(R.id.btn_play1);

        mediaPlayer = new MediaPlayer();

    }


    public void setBtn_play6(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
        mediaPlayer.start();

    }

    public void setBtn_play5(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
        mediaPlayer.start();

    }



    public void setBtn_play4(View v)
    {
       stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
        mediaPlayer.start();

    }

    public void setBtn_play3(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
        mediaPlayer.start();

    }



    public void setBtn_play2(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
        mediaPlayer.start();


    }


    public void setBtn_play1(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
        mediaPlayer.start();
    }

    private void stopPlayer(){
        if(mediaPlayer != null && mediaPlayer.isPlaying())
        {mediaPlayer.stop();}
    }

    }