Reputation: 5955
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?
Upvotes: 14
Views: 60040
Reputation: 1306
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);
}
Upvotes: 9
Reputation: 2695
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()
Upvotes: 5
Reputation: 5825
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();
Upvotes: 6
Reputation: 3305
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1);
mp.start();
And then you get all the start/stop/reset/pause/release methods from mp.
Upvotes: 31