Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created June 10, 2021 00:47
Show Gist options
  • Save codeforfun-jp/2f04f03b75a03a55cadce417e8b7378f to your computer and use it in GitHub Desktop.
Save codeforfun-jp/2f04f03b75a03a55cadce417e8b7378f to your computer and use it in GitHub Desktop.
CTB 11-2
package jp.codeforfun.catchtheball;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
public class SoundPlayer {
private static SoundPool soundPool;
private static int hitSound;
private static int overSound;
private AudioAttributes audioAttributes;
public SoundPlayer(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(audioAttributes)
.setMaxStreams(2)
.build();
} else {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
}
hitSound = soundPool.load(context, R.raw.hit, 1);
overSound = soundPool.load(context, R.raw.over, 1);
}
public void playHitSound() {
soundPool.play(hitSound, 1.0f, 1.0f, 1, 0, 1.0f);
}
public void playOverSound() {
soundPool.play(overSound, 1.0f, 1.0f, 1, 0, 1.0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment