我正在尋找一個非常簡單的播放音效的代碼片段。到目前爲止,我有這樣的代碼:如何在Android中播放音效
SoundManager snd;
int combo;
private void soundSetup() {
// Create an instance of the sound manger
snd = new SoundManager(getApplicationContext());
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the samples from res/raw
combo = snd.load(R.raw.combo);
}
private void playSound() {
soundSetup();
snd.play(combo);
}
但是,由於某種原因,當我使用playSound()
方法,沒有任何反應。音頻文件位於正確的位置。
對於單聲音使用'MediaPlayer'和高性能回放(例如在有很多聲音的遊戲中)使用'SoundPool'。爲了有效的內存管理,你應該使用一個庫來處理這些類:https://github.com/delight-im/Android-Audio – caw