Search code examples
javaandroidaudio-playerandroid-music-player

Musicplayer with onebutton


I am new to android. I'm trying to create a music player which will play specific MP3 with one button(play/pause).I want button's background change between two image when click


Solution

  • try this.... mp is mediaplayer object use your image at btn_play and btn_pause

    btnPlay.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // check for already playing
                    if(mp.isPlaying()){
                        if(mp!=null){
                            mp.pause();
                            // Changing button image to play button
                            btnPlay.setImageResource(R.drawable.btn_play);
                        }
                    }else{
                        // Resume song
                        if(mp!=null){
                            mp.start();
                            // Changing button image to pause button
                            btnPlay.setImageResource(R.drawable.btn_pause);
                        }
                    }
    
                }
            });