I am developing a game and playing audio sounds using SoundPool and MediaPlayer
. Every time when any audio sound开发者_如何学C plays, it shows error in LogCat.
public class MusicSoundResources {
private static final String CLASS_TAG = "SoundResources";
private static MusicSoundResources _instance;
public static HashMap<String, MediaPlayer> mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;
public MusicSoundResources() {
}
public static void initSounds(Context theContext) {
mContext = theContext;
mSoundPoolMap = new HashMap<String, MediaPlayer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
loadSounds();
}
private static void loadSounds() {
mSoundPoolMap.put("swallowed", MediaPlayer.create(mContext, R.raw.swallowed));
}
public static void playSound(String soundTrackIndex, boolean loop) {
MediaPlayer mediaPlayer = mSoundPoolMap.get(soundTrackIndex);
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(streamVolume, streamVolume);
mediaPlayer.setLooping(loop);
mediaPlayer.start();
}
}
These are two logs which display:
05-31 15:02:56.400: ERROR/AudioTrack(2384): getAudioMode[0]
05-31 15:02:56.400: ERROR/AudioR2VS(2384): The 11025 sample rate is not supported by R2VS solution.
Same problem occurs when I use SoundPool.
Tell me what is the supported sample rate.
Finally I got the answer for this. This error comes when we play sounds of bit rate higher than 11025 Hz. This is an an internal error of android media player.
Tha last parameter of the play()
-method is a float
, not an int
:
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
This should work:
mSoundPool.play(mSoundPoolMap.get("fire"), streamVolume, streamVolume, 1, 0, 1.0);
精彩评论