Friday, February 20, 2015

Android play Short Sound Clips


As part of an app, which had to play a short sound. There is a possibility that we can use SoundPool. 

Below is some code snippet for this: 

SoundPool soundPool;
HashMap soundPoolMap;
int soundID = 1;
Button sound1;

@override onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
soundPool = new SoundPool(4,AudioManager.STREAM_MUSIC,100);
soundPoolMap = new HashMap();
soundPoolMap.put(soundID,soundPool.load(this,R.raw.click,1));
sound1 = (Button) findViewByID(R.id.Beaver);
sound1.setOnClickListener(new View.onClickListener()
{
public void onClick(View v)
{
AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
float curVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float leftVolume = curVolume / maxVolume;
float rightVolume = curVoluem /maxVolume
int priority  = 1;
int no_loop = 0;
float normalPlaybackRate = 1f;
soundPool.play (soundID,leftVolume, rightVolume,priority,no_loop,normal_playback_rate);
}
});
}

References:

No comments:

Post a Comment