0

I'm using the following code to play a mp3 from raw folder but nothing happens! can anyone help me? thanks

    MediaPlayer mp = new MediaPlayer();
    mp = MediaPlayer.create(ShapesActivity.this, R.raw.circle);
    mp.start();
    mp.release();
xry
  • 697
  • 1
  • 5
  • 21
  • possible duplicate of [How do I play an mp3 in the res/raw folder of my android app?](http://stackoverflow.com/questions/4162230/how-do-i-play-an-mp3-in-the-res-raw-folder-of-my-android-app) – scriptocalypse Jan 17 '15 at 18:35

2 Answers2

0

You are releasing the mediaplayer right after you start it, so the sound doesn't play. You need to remove mp.release()

TuomasK
  • 502
  • 3
  • 11
0

As pointed out @TuomasK you release the media before playing it. You should implement OnCompletionListener to release MediaPlayer properly.

You can do it like this:

MediaPlayer mMp = MediaPlayer.create(ShapesActivity.this, R.raw.circle);
mMp.start();

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        mp.reset();
        mp.release();
    };
});
issathink
  • 1,220
  • 1
  • 15
  • 25