gpt4 book ai didi

android - SoundPool 不播放声音

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:09:56 24 4
gpt4 key购买 nike

我制作了一个 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);
}

这个类是从我的 Activity 通过以下方式调用的:

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

最佳答案

显然它不适用于我的 Activity ,但在事件发生时调用它(Intents/Keypress)它似乎有效

关于android - SoundPool 不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3322560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com