/SoundPlayer.java Secret
Created
June 10, 2021 00:47
CTB 11-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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