Kiran
Kiran

Reputation: 23

Getting error E/MediaPlayer: error (1, -19) when trying to stop and play sound again

I have muliple buttons and on clicking them I want a sound. My code goes like this

button.setOnClickListener(new Button.OnClickListener() {
          @Override
          public void onClick(View view) {                  
            if(mSound != null && mSound.isPlaying()){
                mSound.stop();
                mSound.reset();
                mSound.release();
                mSound = null;
            }
            mSound = new MediaPlayer();
            mSound = MediaPlayer.create(getApplicationContext(), R.raw.button);
            mSound.start();
            }
        });

In OnCreate, I have intitalized mSound like this,

mSound = new MediaPlayer();
mSound = MediaPlayer.create(this, R.raw.button);

I am getting error (1,-19) as well as (0,38).

Note: This is not duplicate question. I tried each answer from all the questions that are asked before but nothing worked.

Upvotes: 2

Views: 6789

Answers (2)

Ritveak
Ritveak

Reputation: 3778

I was getting same error, while I was playing some audio files repeatedly. I was making a simple English to french phrase book, having 8 buttons for 8 common phrases, the audio files were very small. On closely monitoring the log I found that there is some limitation from the increment in code cache. After playing the audio repeatedly after some time I got this:

2019-12-10 12:37:32.561 29888-29893/com.example.gridlayout I/zygote: Do partial code cache collection, code=22KB, data=30KB
2019-12-10 12:37:32.567 29888-29893/com.example.gridlayout I/zygote: After code cache collection, code=22KB, data=30KB
2019-12-10 12:37:32.567 29888-29893/com.example.gridlayout I/zygote: Increasing code cache capacity to 128KB

On repeating further more I got this:

2019-12-10 12:37:46.008 29888-29893/com.example.gridlayout I/zygote: Do partial code cache collection, code=53KB, data=57KB
2019-12-10 12:37:46.009 29888-29893/com.example.gridlayout I/zygote: After code cache collection, code=53KB, data=57KB
2019-12-10 12:37:46.009 29888-29893/com.example.gridlayout I/zygote: Increasing code cache capacity to 256KB

And on doing even more, I got:

2019-12-10 12:38:23.596 29888-29905/com.example.gridlayout E/MediaPlayerNative: error (1, -19)
2019-12-10 12:38:23.596 29888-29888/com.example.gridlayout E/MediaPlayer: Error (1,-19)

I repeated this by patiently waiting for the short audio to complete and then play again, making sure that the cache isn't increased due to simultaneously playing over and over again.

What I inferred is that, to run the audio files which are huge, code cache needs to be increased, it does automatically to some extent and gives out error after a limit(256KB in my case). Pretty sure the cache can be increased somehow...

I experimented the robustness on my phone and it worked perfectly fine, I guess in our phones, they clear cache automatically after completion. Try running your app on mobile instead of emulator.

My conclusion: Code cache limits the performance for sound playing. Either increase the code cache limit (I tried shallow searching, couldn't find, but search it properly, I guess you will find it somewhere), or try running it directly on your Mobile.

Upvotes: 1

Syed Waqas Bukhary
Syed Waqas Bukhary

Reputation: 5340

According to the docs failure happen due to several reasons, the main ones are

failure to call release()

and it is recommended that you catch your error and recover.

Some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances.

Try this answer or something better to catch the error. Or try the following code, to release the object in memory.

mSound = MediaPlayer.create(getApplicationContext(), R.raw.button);
mSound.start();

mSound.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        mp.release();

    };
});

Upvotes: 6

Related Questions