简体   繁体   中英

Changing audio button from play to pause when clicked and vice versa

I want a play button in my app that when clicked plays the audio and changes to pause, and when the pause button is clicked the audio stops and the button changes to play again and so on. But the button is not working as expected. Please help. I'm adding my java code below.

public class surah extends AppCompatActivity {

    MediaPlayer mp = new MediaPlayer();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surah);
        mp=MediaPlayer.create(surah.this, R.raw.surahkahf);


        final ImageView audio = (ImageView)findViewById(R.id.btn);

        audio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mp.isPlaying()){
                    mp.stop();
                    audio.setBackgroundResource(R.drawable.play);
                    try {
                        mp.prepare();
                    }catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else {
                    mp.start();
                    audio.setImageResource(R.drawable.pause);

                }
            }
        });
    }
}

First, as aborocz mentions in comments, you probably intend to pause playback instead of stop it, so the method you want to use is pause() . In that case you would not need to prepare the MediaPlayer again, and it will start from the same place it was paused when playback is resumed.

Second, the isPlaying() method is not particularly appropriate for this purpose. There are race conditions that prevent the desired behavior. From the Android MediaPlayer documentation :

Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls to isPlaying(), and it can be a number of seconds in the case of streamed content.

Instead, store your own boolean value.

public class surah extends AppCompatActivity {

    private MediaPlayer mp;
    private boolean isMediaPlaying = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surah);
        mp = MediaPlayer.create(surah.this, R.raw.surahkahf);

        final ImageView audio = (ImageView)findViewById(R.id.btn);

        audio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMediaPlaying) {
                    mp.pause();
                    audio.setBackgroundResource(R.drawable.play);
                    isMediaPlaying = false;
                } else {
                    mp.start();
                    audio.setImageResource(R.drawable.pause);
                    isMediaPlaying = true;
                }
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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