简体   繁体   English

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

[英]i want to stop current playing audio when user click on next play button

I am facing problem in my app. 我的应用程序遇到问题。 All the audios are playing well but the problem is when I press on the first button to start playing audio, it plays it and if I click on next play button that also starts playing but the first audio does not stop. 所有音频都播放良好,但问题是当我按第一个按钮开始播放音频时,它会播放它,如果我单击也开始播放的下一个播放按钮,但第一个音频不会停止。 how to stop that. 如何停止。 please help 请帮忙

在此处输入图片说明 Here, my app java code : 在这里,我的应用程序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();
        }
        }

Introduce new method call like stopAllplayers 引入新的方法调用,例如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();}
}

then call this method in all of your play methods. 然后在所有播放方法中调用此方法。

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

do this for all setBtn_play1, setBtn_play2..... 对所有setBtn_play1,setBtn_play2 .....执行此操作

Use below code with one media player 在一个媒体播放器上使用以下代码

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();}
    }

    }

暂无
暂无

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

相关问题 当我点击android中的按钮时,我想一个接一个地播放音频 - I want to play audio one after another when i click button in android 我正在制作一个应用程序,如果您单击一个按钮,则会播放音频,但最后两个按钮不会播放音频 - I am making an app which if you click on a button an audio will play but the audio is not playing in last two buttons 我制作了一个应用程序,如果您单击一个按钮,音频将播放,如果您单击两次,它将停止,但停止 function 仅工作一次 - I made an app in which if you click a button an audio will play and if you click twice it will stop but the stop function is working only once 为什么当我停止从按钮动作侦听器播放音频时出现此错误(音频从线程运行)整个代码在里面 - why i get this error when i stop playing an audio from button action listener(the audio ran from Thread)the whole code is inside 我想在按钮单击时停止功能 - I want to stop a function on button click 在android Studio中,我想要当我单击按钮时,下一个活动/片段应该来自右侧 - In android Studio, I want when i click on button , next activity/fragment should come from right side 我希望声音在用户单击“主页”按钮时停止,但在调用新的内容视图时不停止 - I want the sound to stop when the user clicks the home button, but not when a new content view is called 当我调用当前的下一个片段表格时,在单击按钮时选项卡选择不会更改 - When I call next fragment form current the tab selection will not change on Button click 我想写在文本框中,如果它显示在页面上,如果它没有显示在页面上,则单击下一步按钮,然后直接单击下一步按钮 - i want to write in textbox if it is display on page and click on next button if it is not display on the page then directly click on next button 单击两次按钮时如何停止Mediaplayer两次播放音频文件 - How to stop Mediaplayer from Playing audio files twice when button is clicked twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM