2

I get a path of music file from my listview using startActivityForResult and then set that string for MediaPlayer with MediaPlayer.setDataSource(STRING); First time I play the song with button it plays all right, and if I click button while song is playing it will reset and play again.

  if (mMediaPlayer.isPlaying()) {
                  mMediaPlayer.reset();
         }

But if I tap on button playback is completed it will not play again.

startActivityForResult

Intent i = new Intent(MainActivity.this, Activityone.class);
                    startActivityForResult(i, 0);
                    mMediaPlayer.reset();

In Listview :

 music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
String filename = musiccursor.getString(music_column_index);
Intent person = new Intent();
Bundle backpack = new Bundle();
backpack.putString("arnswer", filename);
person.putExtras(backpack);
setResult(RESULT_OK, person);
finish();

onActivityResult :

  if (resultCode == RESULT_OK){
               if(requestCode == 0) {
                // handling code of startActivityForResult of first button
               Bundle basket = data.getExtras();
                 s = basket.getString("arnswer");
    }

and Button to start MediaPlayer :

public void pbutton1(View view) {
       try {
           if (mMediaPlayer.isPlaying()) {
                 mMediaPlayer.reset();
        }
         mMediaPlayer.setDataSource(s); //using string here
         mMediaPlayer.prepare();
        mMediaPlayer.start();
   } catch (Exception e) {

   }

LogCat :

enter image description here

Tom Jackson
  • 230
  • 3
  • 17

2 Answers2

1

I think you need to mMediaPlayer.seekTo(0) in order to replay, because it has finished.

See the android doc page.


Edit:

Looking the diagram enter image description here between Started <-> PlaybackCompleted seems to be only one action in order to restart the player... you do not need prepare which you have :( ...

I understand the reason why you want prepare so I suggest OnCompletetionListener to mMediaPlayer.stop() so as to move to Stopped state and be ready for next prepare()!

madlymad
  • 6,367
  • 6
  • 37
  • 68
0

This code works perfectly. I hope to help you.

public class PianoActivity extends Activity {

private MediaPlayer mediaPlayer = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_piano);
    setupUI();
}

@Override
protected void onPause() {
    super.onPause();
    if (mediaPlayer != null) {
        mediaPlayer.release();
        mediaPlayer = null;
    }
}

private void setupUI() {
    findViewById(R.id.doo).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            managerOfSound();
        }
    });
}

private void managerOfSound() {
    mediaPlayer = MediaPlayer.create(this, R.raw.doo);
    if (!mediaPlayer.isPlaying()) {
        mediaPlayer.start();
    } else {
        mediaPlayer.stop();
    }
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.reset();
            mp.release();
        }
    });
}

}

yaircarreno
  • 4,002
  • 1
  • 34
  • 32