I am developing a game application in android. I have designed all the views and implemented all the functionality. Now in the last screen I have to play sounds in android. Can anybody tell me how to pursue with it?
5 Answers
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1);
mp.start();
And then you get all the start/stop/reset/pause/release methods from mp.

- 3,245
- 7
- 55
- 84
-
i should add MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1); mp.start(); on paly button click?? yosh??? Thanks in advance Regards Tushar – Tushar Feb 01 '11 at 12:23
-
Well, mp.start() is what you need on button click. Everything else you can declare earlier. You might want to check http://developer.android.com/reference/android/media/MediaPlayer.html – yosh Feb 01 '11 at 12:27
-
not really :D just prepare file, put in /res/raw like Sriram wrote, include "import android.media.MediaPlayer;" and it should work, in my above code AudioFile1 is the file name in /raw (can be AudioFile1.wav or mp3 or anything that is supported, you just don't add extension) – yosh Feb 01 '11 at 12:32
-
1Remember to use soundpool if you are playing short sound clips for games – Confuse Jun 01 '15 at 04:54
I would suggest SoundPool for seamless playback in android because mediaPlayer first loads the whole sound data in memory than play, so it produces some lag when we switch among sounds frequently.
SoundPool is a better option with small size sound file, and produces best result with .ogg media file.
SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound, 1);
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
}
From Android Developer page: http://developer.android.com/guide/topics/media/index.html
Playing from a Raw Resource Perhaps the most common thing to want to do is play back media (notably sound) within your own applications. Doing this is easy:
Put the sound (or other media resource) file into the res/raw folder of your project, where the Eclipse plugin (or aapt) will find it and make it into a resource that can be referenced from your R class Create an instance of MediaPlayer, referencing that resource using MediaPlayer.create, and then call start() on the instance:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
To stop playback, call stop(). If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again. (create() calls prepare() the first time.)
To pause playback, call pause(). Resume playback from where you paused with start().
Playing from a File or Stream You can play back media files from the filesystem or a web URL:
Create an instance of the MediaPlayer using new Call setDataSource() with a String containing the path (local filesystem or URL) to the file you want to play First prepare() then start() on the instance:
Like this
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(PATH_TO_FILE);
mp.prepare();
mp.start();

- 18,405
- 4
- 60
- 77

- 5,737
- 2
- 35
- 53
By using Media Player you can achieve this.
here is the steps::
1.- Create a new folder called "raw" inside "debug" folder. To create it right clic on "debug" folder > new > directory > and in name it "raw" :
To add files just drag the .wav/.mp3 files to "raw" folder.
2.- Import media player:
import android.media.MediaPlayer;
3.- Declare MediaPlayer global variable(s):
public MediaPlayer mp1;
4.- Inside onCreate method, asign the corresponding sounds:
mp1 = MediaPlayer.create(MainActivity.this, raw.my_sound_name);
5.- Finally, you can use methods...
mp1.start() / mp1.stop() / mp1.pause()

- 18,405
- 4
- 60
- 77

- 2,609
- 1
- 22
- 22
-
What if the sound finished before I stop the media player. Does he stop automatically? – Stanimir Yakimov Feb 23 '15 at 09:56