I have two devices I can test my game on: A US Celluar phone (SCH-R880) and a Kindle Fire, the Kindle Fire being a lot more powerful than the phone.
I have several short (Less than or about 1 second) sound effects. To save on memory I load, play, and release some of these sound effects. On the phone they play (mostly) as expected. On the Kindle Fire, however, they’re cut short. The really short sounds are cut off so quickly I don’t hear anything. The ones that are loaded at setup and remain, however, play fine.
Anyone have any idea what’s going on here? Am I somehow releasing my media too soon? Below is one instance of this. On the phone I hear “Level two!” but on the Kindle I hear something like “Lev tw.”
mpNum = null;
try
{
switch (level)
{
case 2:
mpNum = MediaPlayer.create(contxt, R.raw.l2); break;
case 3:
mpNum = MediaPlayer.create(contxt, R.raw.l3); break;
case 4:
mpNum = MediaPlayer.create(contxt, R.raw.l4); break;
case 5:
mpNum = MediaPlayer.create(contxt, R.raw.l5); break;
case 6:
mpNum = MediaPlayer.create(contxt, R.raw.l6); break;
case 7:
mpNum = MediaPlayer.create(contxt, R.raw.l7); break;
case 8:
mpNum = MediaPlayer.create(contxt, R.raw.l8); break;
default:
return;
}
MediaPlayer vLevel = MediaPlayer.create(contxt, R.raw.level);
vLevel.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
vLevel.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mpl)
{
mpNum.start();
mpl.release();
}
});
mpNum.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}
catch (Exception e) {}
In an attempt to try to fix this, I tried SoundPool, but it doesn’t work; I hear nothing. Below is what I did to try to play the music with SoundPool:
SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
soundPool.load(contxt, R.raw.song2, 1);
AudioManager mgr = (AudioManager)contxt.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeMax;
soundPool.play(1, volume, volume, 1, 5, 2);
UPDATE
I’ve noticed when a sound that should be playing (but doesn’t) has this error that comes up:
AudioPolicyManager: stopOutput() oldDevice 2
AudioPolicyManager: [getDeviceForStrategy] strategy : 0,forceUse(0)
This is strange, and not a good fix, but the only fix I found so far. If I remove
mp.setOnPreparedListenerandmp.setOnCompletionListenerand just havemp.start(), it works.