Android Media Player play/pause Button

前端 未结 7 1464
孤独总比滥情好
孤独总比滥情好 2020-12-24 13:03

In my project, I am playing music file in android media player by using the following code

MediaPlayer mPlayer = MediaPlayer.create(MyActivity.this, R.raw.my         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-24 13:11

    Inside the button click check for mediaPlayer.isPlaying(). This will return true if the media player is playing else false.

    So now with this, flag value you can make a if statement and switch to play or pause like this,

    button.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View view) {
    
                if (mediaplayer.isPlaying()) {
                    mediaplayer.pause();
                } else {
                    mediaplayer.start();
                }
            }
        });
    

提交回复
热议问题