I have a game and I use separate MediaPlayer for each sound
Here it is :
public class SoundSample {
public final static String CORRECT="sounds/correct.wav";
public final static String PASS="sounds/pass.wav";
public final static String CLOCK_OUT="sounds/clock_out.mp3";
public final static String ALARM="sounds/alarm.mp3";
public final static String COUNTDOWN_TO_START="sounds/countdown_to_start.mp3";
public final static String GO="sounds/go.mp3";
private String assetFile;
private MediaPlayer mediaPlayer;
public SoundSample(String sound){
assetFile = sound;
try {
AssetFileDescriptor afd = MainActivity.activity.getAssets().openFd(sound);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.start();
}
public void release(){
mediaPlayer.release();
mediaPlayer.reset();
mediaPlayer = null;
}
}
so, I use
correctSound = new SoundSample(SoundSample.CORRECT);
and then
correctSound.play();
So, at a second use it stops playing on many phones and never play again. I mean, debugger shows that mediaPlayer.start();
is executed but the sounds never play again.
it works well, however , on every Android 6 phone I tested, so it's probably only a android 6- or 5- issue
Is there any way I can correct this? I know I can use SoundPool, but it also has some issues