4

我制作了一个 SoundManager 类,它使用 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);
4

2 回答 2

2

显然它不适用于我的活动 onCreate 但在事件发生时调用它(Intents / Keypress)它似乎工作

于 2010-07-24T04:49:29.623 回答
1

我遇到了这个问题,最后通过在支持音频的模拟器上运行解决了!

不敢相信它是那么简单!

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

于 2012-05-18T19:55:18.410 回答