E/MediaPlayer: error (1, -19)

2019-02-19 09:36发布

问题:

I'm creating a simple soundboard to play sounds when a user clicks a button. Problem is, if the button is pressed enough ( usually around 10 times ) it will eventually stop playing and show the error E/MediaPlayer: error (1, -19) what am I doing wrong? My code that plays the sound:

private void playSound(int soundID){
    final MediaPlayer mp = MediaPlayer.create(this,soundID);
    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            mp.start();

        }
    });
}

回答1:

private void playSound(int soundID){
  final MediaPlayer mp = MediaPlayer.create(this,soundID);
  mp.start();
  mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mp.release();
        }
  });
}


回答2:

You should use Soundpool. It's created exactly for playing short sound effects. And it's much simpler to use than MediaPlayer. MediaPlayer should only be used for playing regular/large music.

See here for example: http://www.vogella.com/tutorials/AndroidMedia/article.html#tutorial-play-sounds-via-soundpool

Note: No need to use onTouch as in the example, you can just use onClick for simplicity.