package gpwiki.demo;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager
{
private Context pContext;
private SoundPool sndPool;
private float rate = 1.0f;
private float masterVolume = 1.0f;
private float leftVolume = 1.0f;
private float rightVolume = 1.0f;
private float balance = 0.5f;
// Constructor, setup the audio manager and store the app context
public SoundManager(Context appContext)
{
sndPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
pContext = appContext;
}
// Load up a sound and return the id
public int load(int sound_id)
{
return sndPool.load(pContext, sound_id, 1);
}
// Play a sound
public void play(int sound_id)
{
sndPool.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
// Set volume values based on existing balance value
public void setVolume(float vol)
{
masterVolume = vol;
if(balance < 1.0f)
{
leftVolume = masterVolume;
rightVolume = masterVolume * balance;
}
else
{
rightVolume = masterVolume;
leftVolume = masterVolume * ( 2.0f - balance );
}
}
public void setSpeed(float speed)
{
rate = speed;
// Speed of zero is invalid
if(rate < 0.01f)
rate = 0.01f;
// Speed has a maximum of 2.0
if(rate > 2.0f)
rate = 2.0f;
}
public void setBalance(float balVal)
{
balance = balVal;
// Recalculate volume levels
setVolume(masterVolume);
}
// Free ALL the things!
public void unloadAll()
{
sndPool.release();
}
}
Code (Android-Java) - DemoMain.java
package gpwiki.demo;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class DemoMain extends Activity
{
SoundManager snd;
int laser, explode, pickup, meow, bark, moo, test;
OnSeekBarChangeListener barChange;
OnClickListener buttonClick;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of our sound manger
snd = new SoundManager(getApplicationContext());
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the samples from res/raw
test = snd.load(R.raw.test);
laser = snd.load(R.raw.laser);
explode = snd.load(R.raw.explosion);
pickup = snd.load(R.raw.pickup);
meow = snd.load(R.raw.cat);
bark = snd.load(R.raw.barkloud);
moo = snd.load(R.raw.cow);
// Create a seek bar handler
barChange = new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
switch (seekBar.getId())
{
case R.id.VolBar1:
snd.setVolume((float)progress/100.0f);
break;
case R.id.BalBar:
snd.setBalance((float)progress/100.0f);
break;
case R.id.SpeedBar:
snd.setSpeed((float)progress/100.0f);
break;
}
}
};
// Set our handler as the ChangeListener for the seekbar controls
SeekBar sb;
sb = (SeekBar)findViewById(R.id.SpeedBar);
sb.setOnSeekBarChangeListener(barChange);
sb = (SeekBar)findViewById(R.id.BalBar);
sb.setOnSeekBarChangeListener(barChange);
sb = (SeekBar)findViewById(R.id.VolBar1);
sb.setOnSeekBarChangeListener(barChange);
}
// Button listener assigned in XML layout
public void clickHandler(View v)
{
int id = v.getId(); // Use the button id to determine which sample should be played
switch (id)
{
case R.id.button1:
snd.play(test);
break;
case R.id.button2:
snd.play(explode);
break;
case R.id.button3:
snd.play(pickup);
break;
case R.id.button4:
snd.play(meow);
break;
case R.id.button5:
snd.play(bark);
break;
case R.id.button6:
snd.play(moo);
break;
}
}
}