0

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

Vlad Alexeev
  • 2,072
  • 6
  • 29
  • 59

2 Answers2

1

You have to recreate the media player , try like below

    if (mediaPlayer.isPlaying()) {
           mediaPlayer.reset();
            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.yourAudio);
        }

  mediaPlayer.start();
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • oh, you mean, set data source each time? isn't that .. too much of a work for quick sounds? – Vlad Alexeev Jan 20 '17 at 08:28
  • That's correct. I just want to add to this answer. @user2976267 do you really need `MediaPlayer`? Having many separate `MediaPlayers` will be heavy for the system. If your sound is small, you can use `SoundPool` to handle many small sound files. –  Ekalips Jan 20 '17 at 08:28
  • @user2976267 read this http://stackoverflow.com/questions/13527134/audiotrack-soundpool-or-mediaplayer-which-should-i-use (Elvis Popovic's ansver) –  Ekalips Jan 20 '17 at 08:31
  • Sorry, this answer didn't help. recreating media player didn't do the thing. Seems to me I'll have to use SoundPools – Vlad Alexeev Jan 20 '17 at 08:35
  • try creating a folder named raw under res folder, add your audio file in it. and give your audio file name at `R.raw.yourAudio` with out extension and check if working – Manohar Jan 20 '17 at 08:42
0

Try to add code mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

Mick
  • 61
  • 3