Playing sound FX for a game

Quickly share your Android Code Snippets here...

Playing sound FX for a game

Postby ninor » Sun Oct 12, 2008 12:42 am

Playing sound FX for a game

This only loads one sound but you can load as many as you want.
Put an mp3 in the res/raw directory called Explosion.mp3

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static final int SOUND_EXPLOSION = 1;
  2.  
  3. public static final int SOUND_YOU_WIN = 2;
  4.  
  5. public static final int SOUND_YOU_LOSE = 3;
  6.  
  7.  
  8.  
  9. private SoundPool soundPool;
  10.  
  11. private HashMap<Integer, Integer> soundPoolMap;
  12.  
  13.  
  14.  
  15. private void initSounds() {
  16.  
  17.         soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
  18.  
  19.         soundPoolMap = new HashMap<Integer, Integer>();
  20.  
  21.         soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(), R.raw.explosion, 1));
  22.  
  23. }
  24.  
  25.                
  26.  
  27. public void playSound(int sound) {
  28.  
  29.         AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
  30.  
  31.         int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
  32.  
  33.         soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, 0, 1f);
  34.  
  35. }
  36.  
  37.  
  38.  
  39. public void update() {
  40.  
  41.       if (isExploding()) {
  42.  
  43.             playSound(SOUND_EXPLOSION);
  44.  
  45.      }
  46.  
  47. }
  48.  
  49.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


NOTE: There are some issues with this object.
Read this thread for more info.

SDK Version compatible: 0.9, 1.0

Thanks to rbgrn for creating and sharing this snippet!
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Top

Postby alpesh_harsoda » Tue Nov 18, 2008 8:01 am

hi ninor,
i try this code its working very nicely.but when i put some wav file instead of mp3 then error occured.
wav file not supported in android?
thnks,
Alpesh
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby MrSnowflake » Tue Nov 18, 2008 11:22 am

There isn't a very good reason of using wav's over mp3's/oggs/aac's/... You are on a mobile device and don't have limited resources.

And for your real question: I don't know :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby alpesh_harsoda » Tue Nov 18, 2008 11:24 am

hello mrsnowflake,
thnks for reply.
but in my application if u want to use the wav file then what i have to do.?
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby isaackearl » Sun Jan 11, 2009 3:03 am

alpesh_harsoda wrote:hello mrsnowflake,
thnks for reply.
but in my application if u want to use the wav file then what i have to do.?


i would just find a converter and turn it into an mp3
isaackearl
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 8:09 pm

Postby Iam_Fiction » Fri Jan 23, 2009 6:27 pm

Does this still work? I didnt hear a sound when i tried this and the Api isnt even commented...
Iam_Fiction
Freshman
Freshman
 
Posts: 5
Joined: Tue Jan 13, 2009 9:59 pm

Top

Postby suhas.gavas » Wed Feb 25, 2009 1:31 pm

use wave file which is 16 bit
suhas.gavas
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Sep 04, 2008 7:58 am

Postby DeveloperAmit » Fri Mar 13, 2009 2:11 pm

man SoundPool class seems to be pretty unstable when we need to play a single sound simultaneously in a app...it crashes the app.
DeveloperAmit
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Mar 04, 2009 1:26 pm

Postby suhas.gavas » Fri Mar 13, 2009 3:16 pm

sound pool may not be stable but i think developer should use OGG sound file format as it works good as compared to others ....... In my game wav file use to play sometimes and sometimes the sample file used be NOT ready to play......... But when i converted those files to OGG and used them , they worked fine with sound pool i.e sound files are played always and no error of sample not ready type
suhas.gavas
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Sep 04, 2008 7:58 am

the code did not work , i dunno why

Postby fundurianfred » Fri May 22, 2009 4:11 am

I used the code to test but i hear no sound? I am using .mp3 , whats wrong
fundurianfred
Freshman
Freshman
 
Posts: 7
Joined: Sun May 17, 2009 9:20 am

Postby SCM » Mon Apr 12, 2010 2:26 pm

the same here. it isn't working.
SCM
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Feb 19, 2010 1:44 am

Postby suhas.gavas » Mon Apr 12, 2010 4:17 pm

please change the file format to OGG and check.
it will work as it done for me
suhas.gavas
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Sep 04, 2008 7:58 am

Postby SCM » Mon Apr 12, 2010 4:19 pm

yes on the emulator it works but not on the phone
SCM
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Feb 19, 2010 1:44 am

Re: Playing sound FX for a game

Postby Trip » Sat May 22, 2010 4:13 am

ogg are the best format for android games (will increase frame rate using ogg over any other format)

do not use WAV ... ogg are an open file format and supported by more devices ;)

* source android talks on you tube *
Trip
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat May 22, 2010 2:58 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Google [Bot] and 5 guests