0

I play music in my app using MediaPlayer. But music interrupts when I playing another sound (duration of sound is about 1 sec). The interruption is short, only a few ms, but it disturbs me.

What should I do to avoid this interruption?

UPDATE:

Here is code that I use to play short sounds:

private final Map<Integer,MediaPlayer> mediaPlayers = new HashMap<Integer,MediaPlayer>();

void playSound(int resId, boolean looping) {
    MediaPlayer mediaPlayer = mediaPlayers.get(resId);
    if (mediaPlayer != null) {
        mediaPlayer.release();
        mediaPlayers.remove(resId);
    }
    try {
        mediaPlayer = MediaPlayer.create(context, resId);
        if (mediaPlayer == null){
            throw new RuntimeException("Can't create MediaPlayer");
        }
        mediaPlayer.setLooping(looping);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    mediaPlayers.put(resId, mediaPlayer);
}

UPDATE2:

I solved the problem. The problem was that the short sound was in mp3 format, but all the other sounds in my app were in ogg format. So I converted this sound in ogg format and the music is no longer interrupted.

Leonid Semyonov
  • 387
  • 3
  • 9

0 Answers0