2010-07-23 188 views
4

我做了一個SoundManager類,它使用SoundPool函數來循環播放特定的聲音,但是我無法弄清楚它爲什麼不播放聲音。SoundPool不播放聲音

public class SoundManager { 
private SoundPool mSoundPool; 
private HashMap<Integer, Integer> mSoundPoolMap; 
private AudioManager mAudioManager; 
private Context mContext; 

public SoundManager() 
{ 

} 

public void initSounds(Context theContext) { 
    mContext = theContext; 
    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
    mSoundPoolMap = new HashMap<Integer, Integer>(); 
    mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);   
} 

public void addSound(int Index, int SoundID) { 
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1)); 
} 

public void playSound(int index) { 
    int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
} 

public void playLoopedSound(int index) { 
    int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
} 

}

該類正從我的活動可以通過名爲:

private SoundManager mSoundManager; 
mSoundManager = new SoundManager(); 
mSoundManager.initSounds(getBaseContext()); 
mSoundManager.addSound(1, R.raw.sound); 
mSoundManager.playSound(1); 

回答

2

顯然,這是行不通的onCreate我的活動,但稱這是當一個事件發生(意圖/按鍵時)它似乎工作

1

我有這個問題,並最終通過在支持音頻的模擬器上運行解決它!

簡直不敢相信!

窗口> AVD管理器>編輯您的AVD(我創建了一個新的安全端,因爲我從快照運行)>硬件>新>音頻播放支持