By the way: try to use .ogg files, they are played better by android.
Also i had problems with playLoop(int resid),
sometimes the sample is looped, and sometimes it is not.
A possible usage of the SoundEffects class is by subclassing it.
The code from my example ( i am making a coooool game! :-) :
public class GameSounds extends SoundEffects {
private static GameSounds INSTANCE;
public static GameSounds getInstance(Context ctx) {
//return new GameSounds(ctx);
if(INSTANCE==null || INSTANCE.released)INSTANCE=new
GameSounds(ctx);
return INSTANCE;
}
private GameSounds(Context context) {
super(17, context);
addSound(R.raw.engage);
addLoopSound(R.raw.alarm2);
addSound(R.raw.fire);
addSound(R.raw.explosion1);
addSound(R.raw.explosion2);
addSound(R.raw.smallexplosion);
addSound(R.raw.mediumexplosion);
addSound(R.raw.lasergun);
addLoopSound(R.raw.machinegun1);
//addSound(R.raw.);
addSound(R.raw.heal);
addSound(R.raw.enemydeath);
addSound(R.raw.bossdeath);
//addSound(R.raw.bigexplosion);
//addSound(R.raw.shortshoot1);
addSound(R.raw.shortshoot2);
//addSound(R.raw.gunload);
}
public void smallExplosion() {
play(R.raw.smallexplosion);
}
public void explosion() {
play(R.raw.mediumexplosion);
}
public void explosion2() {
play(R.raw.explosion2);
}
public void engage() {
play(R.raw.engage);
}
public void heal() {
play(R.raw.heal);
}
public void nextWave() {
play(R.raw.heal);
}
public void fireVoice() {
play(R.raw.fire);
}
public void fire() {
play(R.raw.smallexplosion);
}
public void fire1() {
//play(R.raw.shortshoot1);
}
public void fire2() {
//play(R.raw.shortshoot1);
}
public void startFire() {
playLoop(R.raw.machinegun1);
}
public void stopFire() {
stop(R.raw.machinegun1);
}
public void startAlarm() {
playLoop(R.raw.alarm2);
}
public void stopAlarm() {
stop(R.raw.alarm2);
}
public void gameover() {
play(R.raw.bossdeath);
}
}
On 12 mrt, 21:37, TjerkW <[email protected]> wrote:
> Is the API of theSoundPoolthe same in cupcake?
> I hope so.
>
> For all the people who need a reliablesoundpool, i have made a class
> that handles the details withsoundpooland is easy to use:
>
> import java.util.HashMap;
> import android.content.Context;
> import android.media.AudioManager;
> import android.media.SoundPool;
> import android.os.Vibrator;
> import android.util.Log;
>
> public class SoundEffects {
> public boolean released=false;
> protected Context context;
> privateSoundPoolsoundPool;
> private int volume;
> private HashMap<Integer, Integer> soundPoolMap;
>
> protected SoundEffects(int size, Context context) {
> this.context=context;
> // keep maxStreams high, ugly hack because pre cupcake
> android could
> deadlock
> // withsoundpool
> soundPool=newSoundPool(size+20, AudioManager.STREAM_MUSIC, 40);
> soundPoolMap = new HashMap<Integer, Integer>();
> AudioManager mgr=(AudioManager)context.getSystemService
> (Context.AUDIO_SERVICE);
> volume=mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
> }
>
> /*
> public boolean isLoaded() {
> soundPool.
> }
> */
>
> public void addSound(int resid) {
> int soundId=soundPool.load(context, resid, 1);
> soundPoolMap.put(resid, soundId);
> soundPool.setLoop(soundId, 1);
> }
>
> public void addLoopSound(int resid) {
> int soundId=soundPool.load(context, resid, 1);
> soundPoolMap.put(resid, soundId);
> soundPool.setLoop(soundId, -1);
> }
>
> public void play(int resid) {
> Log.i("SoundEffects", "Playing: "+resid);
> int soundId=soundPoolMap.get(resid);
> soundPool.setLoop(soundId, 1);
> soundPool.play(soundId, volume, volume, 1, 0, 1f);
> }
>
> public void playLoop(int resid) {
> int soundId=soundPoolMap.get(resid);
> soundPool.setLoop(soundId, -1);
> soundPool.play(soundId, volume, volume, 1, 0, 1f);;
> }
>
> public void stop(int resid) {
> //soundPool.stop(resid);
> // apperently a bug, workaround
> int soundId=soundPoolMap.get(resid);
> soundPool.setLoop(soundId, 0);
> soundPool.setVolume(soundId, 0f, 0f);
> }
>
> public void release() {
> released=true;
> soundPool.release();
> }
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---