简体   繁体   中英

Change Button icon on click play and pause

i want to add mp3 file to my app with play and pause button , currently i am using play and pause icon but i want to show play and pause button separately , like when user click on play button pause icon should show and vice versa , this is my code to play file

Button play;
play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mp != null && mp.isPlaying()) {
            mp.pause();
            length = mp.getCurrentPosition();
        } else {
            mp = MediaPlayer.create(PunjabiActivity.this, R.raw.japji);
            mp.seekTo((int) length);
            mp.start();
        }
    }
});
play.setImageResource(R.drawable.playIcon);
play.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mp != null && mp.isPlaying()) {
            mp.pause();
            length = mp.getCurrentPosition();
            play.setImageResource(R.drawable.pauseIcon)
        } else {
            mp = MediaPlayer.create(PunjabiActivity.this, R.raw.japji);
            mp.seekTo((int) length);
            mp.start();
            play.setImageResource(R.drawable.playIcon)
        }
    }
});

You should replace your Button both in the layout and in the code with an ImageButton, then after calling mp.pause(); and mp.start(); you should respectively do: play.setImageResource(R.drawable.yourPlayIcon) and play.setImageResource(R.drawable.yourPauseIcon)

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