2

I try to replay a sound when I click on a button. But I get the Error (-19, 0) (what ever this means^^)

My code:

final Button xxx = (Button)findViewById(R.id.xxx);

        xxx.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.plop); 
                mp.start();
            }
        });

What is my mistake?

Phil
  • 646
  • 1
  • 8
  • 23
  • What format is the media file? Supported Android media files are here: http://developer.android.com/guide/appendix/media-formats.html – Brian Var Apr 11 '13 at 14:40
  • 1
    `-19` is `NO_INIT` as far as I can tell. – Michael Apr 11 '13 at 15:01
  • Does the error occur on the first time you click? Otherwize, it could be that you (maybe) don't release the resources (and get out of memory). At least, this is what I get from [here](http://stackoverflow.com/questions/9888510/mediaplayer-error-19-0-after-repeated-plays). – MalaKa Apr 11 '13 at 15:25
  • Error occurs since the first click – Phil Apr 11 '13 at 15:43

4 Answers4

3

I was getting the same problem, I solved it by adding the following code:

mp1 = MediaPlayer.create(sound.this, R.raw.pan1);
mp1.start();
    mp1.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
        mp.release();

        };
    });
Gabriel Kaffka
  • 1,039
  • 8
  • 9
0

You need to release the previous media player before starting the new one.

Declare MediaPlayer as a instance variable and then:

mp = null;
final Button xxx = (Button)findViewById(R.id.xxx);

        xxx.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mp != null) {
                  mp.stop();
                  mp.release();
                }

                mp = MediaPlayer.create(getApplicationContext(), R.raw.plop); 
                mp.start();
            }
        });

Or in your case, since you always play the same sound you don't need to release the player and create a new one, simply reuse the old one.

final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.plop);
mp.prepare(); // Blocking method. Consider to use prepareAsync

final Button xxx = (Button)findViewById(R.id.xxx);

        xxx.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mp.stop();
                mp.start();
            }
        });
Dan Andreasson
  • 15,380
  • 5
  • 29
  • 30
0
    public static MediaPlayer audiostop;
    void stopcard() {
        if (audiostop != null) {
            audiostop.reset();
            audiostop.release();
            audiostop = null;
        }
        audiostop = MediaPlayer.create(this, R.raw.stop);
        audiostop.start();
    }
lacy025
  • 1
  • 1
-1

I sloved this problem by this code:

    public static void playSound() {
    mMediaPlayer = new MediaPlayer();
    try {
        AssetFileDescriptor afd = context.getAssets().openFd("type.mp3");
        mMediaPlayer.setDataSource(afd.getFileDescriptor(),
                afd.getStartOffset(), afd.getLength());
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                arg0.release();
            }
        });
    } catch (IllegalArgumentException | IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

i hope to help you.

Iman Marashi
  • 5,593
  • 38
  • 51
  • 1
    Please consider including some information about your answer, rather than simply posting code. We try to provide not just 'fixes', but help people learn. You should explain what was wrong in the original code, what you did differently, and why your change(s) worked. – Andrew Barber Jan 19 '15 at 21:12