void initSoundPool() {
synchronized (this) {
if (mSoundPool == null) {
mSoundPool = new SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
.build();
mStartSound = mSoundPool.load(mContext, R.raw.start, 1);
mEndSound = mSoundPool.load(mContext, R.raw.end, 1);
mErrorSound = mSoundPool.load(mContext, R.raw.error, 1);
}
}
}
void releaseSoundPool() {
synchronized (this) {
if (mSoundPool != null) {
mSoundPool.release();
mSoundPool = null;
}
}
}
播放:
switch (sound) {
case SOUND_START:
mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_END:
mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_ERROR:
mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
}