public void playSound( ) {
SoundPool soundPool;
if (Build.VERSION.SDK_INT >= 21) {
SoundPool.Builder builder = new SoundPool.Builder();
builder.setMaxStreams(1);
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
builder.setAudioAttributes(attrBuilder.build());
soundPool = builder.build();
} else {
soundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
}
soundPool.load(this, R.raw.du0, 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(1, 1, 1, 0, 0, 1);
}
});
}
R.raw.du0为res\raw目录下放的短音频文件