Pages

Thursday 28 September 2017

SoundPool, SoundPool.Builder(), Supporting All API in Android


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        soundPool = new SoundPool.Builder()
                .setMaxStreams(10)
                .build();

        soundId = soundPool.load(this, R.raw.gun_shot, 1);

    } else {
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
        soundId = soundPool.load(this, R.raw.gun_shot, 1);
    }

}

public void gunShot(View view) {
    soundPool.play(soundId, 1, 1, 1, 0, 1);
}