R
R
rafaelSalihov2019-12-11 18:19:43
Java
rafaelSalihov, 2019-12-11 18:19:43

How to pause the previous media file when clicking on a new one?

I have an android application in java,
when the button is pressed, the function of playing the file in the sound is substituted for the current MediaPlayer, in the same place clearing the memory after playing onCompletion. How to do if one media file is playing, when you click on another button, the second one is played, but the first one stops.

public void soundPlay(MediaPlayer sound) {
       
        sound.start();
        sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();;
            }
        });
       
    }

I tried to make it in this way, too, we give an error if you click on the same sound file twice
public void soundPlay(MediaPlayer sound) {
        if(m != null) {
            m.stop();
        }
        sound.start();
        sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();;
            }
        });
        m = sound;
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question