Skip to content

Instantly share code, notes, and snippets.

@seal308
Created June 21, 2016 04:59
Show Gist options
  • Select an option

  • Save seal308/b4296ea1c9fa38808b774f5f42ba80a0 to your computer and use it in GitHub Desktop.

Select an option

Save seal308/b4296ea1c9fa38808b774f5f42ba80a0 to your computer and use it in GitHub Desktop.
Android sounds
public class MainActivity extends Activity implements View.OnClickListener{
private SoundPool soundPool;
int sample1 = -1;
int sample2 = -1;
int sample3 = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
try{
// Create objects orf the 2 required classes
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor;
// create our 3 fx in memory ready for use
descriptor = assetManager.openFd("sample1.ogg");
sample1 = soundPool.load(descriptor, 0);
descriptor = assetManager.openFd("sample2.ogg");
sample2 = soundPool.load(descriptor, 0);
descriptor = assetManager.openFd("sample3.ogg");
sample3 = soundPool.load(descriptor, 0);
}catch (IOException e){
//catch exception here
}
...
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button:
soundPool.play(sample1, 1, 1, 0, 0, 1);
break;
case R.id.button2:
soundPool.play(sample2, 1, 1, 0, 0, 1);
break;
case R.id.button3:
soundPool.play(sample3, 1, 1, 0, 0, 1);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment