SoundPool не воспроизводит звуки - PullRequest
4 голосов
/ 24 июля 2010

Я создал класс 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);

Ответы [ 2 ]

2 голосов
/ 24 июля 2010

По-видимому, он не работает с OnCreate для моей деятельности, но вызывает его, когда происходит событие (Intents / Keypress), кажется, что он работает

1 голос
/ 18 мая 2012

У меня была эта проблема, и я, наконец, решил ее, запустив эмулятор с поддержкой звука !!

Не могу поверить, что это было так просто!

Окно> Диспетчер AVD> Отредактируйте AVD (я создал новый, чтобы быть в безопасности, потому что я запускал снимок)> Аппаратное обеспечение> Новое> Поддержка воспроизведения аудио

...