0

I have a button when clicked it plays an audio, and while it is playing I can pause it and replay it again and so forth. I have a shake event, where I want to play the audio on shake and replay it when device is shaken again (by first stopping the audio, calling stopAudio?)

I have the following code:

llBtn = (LinearLayout) findViewById(R.id.button);
llBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(MainActivity.this, "LL WAS CLICKED", Toast.LENGTH_SHORT).show();
        //IF AUDIO IS NOT PLAYING... PLAY AUDIO
        if (tvPS.getText() == "Stop Phrase!") {
            StopAudio();
        }
        else {
            PlayAudio();
        }
    }
});
//the same button is clicked to stop, it is currently setting to Pause.
public void StopAudio() {
        mediaPlayer.pause();
        Toast.makeText(MainActivity.this, "STOPPED", Toast.LENGTH_SHORT).show();
        tvPS.setText("Play Phrase!");
    }

public void PlayAudio() {
    //stopPlaying(mediaPlayer);
    tvPS.setText("Stop Phrase!");

    inResId = getResources().getIdentifier("play" , "raw", getPackageName());

    mediaPlayer = MediaPlayer.create(getBaseContext(), inResId);
    mediaPlayer.seekTo(0);
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            //Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
            tvPS.setText("Play Phrase!");
            mediaPlayer.pause();
        }
    });
}
...
public void onShake() {
    // Do stuff!
    Toast.makeText(getBaseContext(), "Motion detected", 
            Toast.LENGTH_SHORT).show();

    if (mediaPlayer == null) {
        PlayAudio();
    }
}

How can I modify the code above to handle Play, and then stop if stopped in the middle and then able to replay it again.

The button click works but I am creating a new instance each time and not recycling. When the device is shaken, it plays the audio twice instead of just once.

Si8
  • 9,141
  • 22
  • 109
  • 221

1 Answers1

1

First of all create the MediaPlayer in onCreate of the context and not in some listener, and don't forget to release it when done, since it consumes a lot of resources,

mediaPlayer.release();
mediaPlayer = null;

Next thing, stop the audio using the stop() function instead of the pause() on completion of the song.

Steps

If you are using a service/activity to play a video create a MediaPlayer instance before it's usage begins, likely in onCreate.

Later use that instance to play/pause/stop the media.

In the onStop of the Service/Activity release the MediaPlayer instance.

Update your playMusic() function to use the MediaPlayer instance you just created, also in the listener use stop() instead of pause()

To change track of an existing MediaPlayer instance

You can use this:

String path = getExternalFilesDir(null).toString()+"/";

mMediaPlayer.setDataSource(path + mediafile);

Link: Changing data source for audio playback using existing MediaPlayer?

Community
  • 1
  • 1
Abhishek
  • 2,959
  • 2
  • 17
  • 24
  • I have multiple audio I am playing which is not listed here the resID is different. Can i create a blank mediaPlayer and use that across the activity? – Si8 Apr 13 '16 at 02:48
  • 1
    I've have edited the answer, have a look – Abhishek Apr 13 '16 at 02:59