SoundPool不播放声音

SoundPool不播放声音

问题描述:

我做了一个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); 

显然,这是行不通的onCreate我的活动,但称这是当一个事件发生(意图/按键时)它似乎工作

我有这个问题,并最终通过在支持音频的模拟器上运行解决它!

简直不敢相信!

窗口> AVD管理器>编辑您的AVD(我创建了一个新的安全端,因为我从快照运行)>硬件>新>音频播放支持