javaandroidxmlandroid-mediaplayer

Android Studio: Start and Stop audio using mediaPlayer


How to start and stop playing the music using the same button. The code below will play the song multiple times (overlaping) if I click it repeatedly.

And another problem is where to insert starTimer and StopTimer function so the starTimer will active is the sound is not playing and stopTimer will active if the sound is playing.

Updated Code:

   public void playFile(View v) {
    if (mediaPlayer == null)
        mediaPlayer = new MediaPlayer();

    try {

        mediaPlayer.setDataSource(question.getAudio());

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(@NotNull MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.prepareAsync();

    }catch (IOException e) {
        e.printStackTrace();
        stopSelf();
    }

    if (mediaPlayer.isPlaying()) {
        //pause music
        mediaPlayer.pause();
    } else {
        //play music
        mediaPlayer.start();
    }
}

Xml file:

<Button
    android:id="@+id/audQuestion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:onClick="playFile"
    android:layout_centerInParent="true"/>

and for the startTimer stopTimer function

public void starTimer() {
    timer = new Timer(Constant.TIME_PER_QUESTION, Constant.COUNT_DOWN_TIMER);
    timer.start();
}

public void stopTimer() {
    if (timer != null)
        timer.cancel();
}

Solution

  • Please define below code at Global:

    private Button play_pause;
    MediaPlayer mediaPlayer; 
    

    Now in onCreate Method:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mediaPlayer = new MediaPlayer();
            play_pause = findViewById(R.id.play_pause);
        }
    

    Play pause button:

    play_pause.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mediaPlayer != null) {
                            if (mediaPlayer.isPlaying()) {
                                mediaPlayer.pause();
                            } else {
                                mediaPlayer.getDuration();
                                mediaPlayer.start();
                            }
                        } 
                    }
                });
    

    When Audio is ready to play:

    private void readyToPlay(Uri uri) {
            try {
                if (mediaPlayer != null) {
                    mediaPlayer.setOnCompletionListener(this);
                    mediaPlayer.setDataSource(activity, uri);//Write your location here
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    Audio is complete then auto stop:

    @Override
        public void onCompletion(MediaPlayer mp) {
            if (mediaPlayer != null) {
                mp.stop();
                mp.release();
            }
        }
    

    Don't forgot to add this line:

    implements MediaPlayer.OnCompletionListener