Why you declare your MediaPlayer
inside playSound
method?
I think when playSound
method execution is done your MediaPlayer cleared from memory, try to declare MediaPlayer as class variable like this:
public class SoundPlayer extends MediaPlayer{
BookInterface ownerActivity;
private MediaPlayer mp;
public SoundPlayer(BookInterface act){
ownerActivity = act;
}
public void playSound(Context context, int resId){
Log.d("Debug", "playSound is called");
try {
mp = MediaPlayer.create(context, resId);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("Debug", "playSound.onCompletion is called");
mp.stop();
mp.release();
ownerActivity.eventHandler();
}
});
mp.start();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("Debug","Exception" + e);
e.printStackTrace();
}
}
}