Android Mediaplayer Error (1, -2147483648)

Android MediaPlayer Error (1,-22)

It seems to issue appears because I didn't call MediaPlayer.release() when onCompletion callback. As a result resources of MP did not released and finally MP crashed with the error.

MediaPlayer Error (1, -1004)


the audio starts but after 2-3 seconds it stops with an Error that is
MediaPlayer error (1, -1004).

First, Lets understand what error (1, -1004) means. -1004 is the error code for IO error. Below reference from MediaPlayer.java source code.

/** File or network related operation errors. */
public static final int MEDIA_ERROR_IO = -1004;

This type of error comes if for some reason, the media player is not able to connect to the server due to network issues. It could be bad internet connectivity at that instance or some network related reason due which the media player was unable to connect to the server. There are some other similar error codes that the media player can throw like time-outs or server died:

/** Some operation takes too long to complete, usually more than 3-5 seconds. */
public static final int MEDIA_ERROR_TIMED_OUT = -110;
/** Media server died.*/
public static final int MEDIA_ERROR_SERVER_DIED = 100;

Now What should I do?

To handle errors generated by media player at run time, you should implement Error Listener. You can handle the error in which ever way as you like for example restart the player.

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
switch(extra){
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
// Do Something
// eg. reset the media player and restart
break;
case MediaPlayer.MEDIA_ERROR_IO:
// Do Something
// eg. Show dialog to user indicating bad connectivity
// or attempt to restart the player
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
//Do Something
//eg. Show dialog that there was error in connecting to the server
// or attempt some retries
break;
}
//You must always return true if you want the error listener to work
return true;
}
});

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

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();

};
});

ANDROID: MediaPlayer shows error (1, -19)

Finally solved it! After a lot of trial & error!!
This is the line that causes it for me:

mMediaPlayer.setAudioSessionId(AudioManager.STREAM_MUSIC);

The API doc for setAudioSessionId doesn't indicate any alternative or any issue on still using it which made it hard to debug. But I saw setAudioStreamType just below it, which is deprecated and suggests setAudioAttributes instead, w/c is added in API 21.

So I changed the above code to this:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mMediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
} else {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}

And now it works!

By the way, as you can see, I checked only against version N (API 24) and above instead of from LOLLIPOP even though setAudioAttributes was added in API 21, because I only encounter this issue on Nougat devices, setAudioSessionId works up to Marshmallow devices for me so... don't wanna break anything that's already working.

I do hope someone would stop by and explain why this happens and what the error really means.

Hope this saves anyone days of headache :)

EDIT:
For anyone Googling around, I also changed the original mMediaPlayer.setAudioSessionId to mMediaPlayer.setAudioStreamType, as pointed out by Eugen Pechanec. See his comment and answer for details.
You would also want to change from checking against Build.VERSION_CODES.N to Build.VERSION_CODES.LOLLIPOP with that.

Android MediaPlayer Error (1,-1010)

Dear Friends the problem was that I try to skip position which is not downloaded yet. Before to seek to that position at first I'm checking if that part is downloaded or not. If not I'm waiting until it will be downloaded and after that seeking to that position. Here is the code:

private Handler mHandlerDownloadUpdate = new Handler();
public static MediaPlayer mMediaPlayer;

if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
} else {
mMediaPlayer.release();
mMediaPlayer = new MediaPlayer();
}

mMediaPlayer.setOnBufferingUpdateListener(bufferingUpdateListener);
mSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

private void initRunnable(final int progress) {
mRunnable = new Runnable() {
@Override
public void run() {
if (progress * 100 / mDuration < mPercentOfDownload) {
mMediaPlayer.seekTo(progress * 1000);
mMediaPlayer.start();
return;
}
mHandlerDownloadUpdate.postDelayed(this, 1000);
}
};

MediaPlayer.OnBufferingUpdateListener bufferingUpdateListener = new
MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
mPercentOfDownload = percent;
if (mDuration != 0) {
int second = percent * mDuration / 100;
mSeekBar.setSecondaryProgress(second);
}
}

SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, final int progress,
boolean fromUser) {
if (mMediaPlayer != null && fromUser) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
seekBar.setProgress(progress);
int a = (progress * 100 / mDuration);
if (a < mPercentOfDownload) {
mMediaPlayer.seekTo(progress * 1000);
mMediaPlayer.start();
} else {
mHandlerDownloadUpdate.removeCallbacks(mRunnable);
initRunnable(progress);
PlayMusicActivity.this.runOnUiThread(mRunnable);
}
}
}
}

android:mediaplayer error(1, -17)

Please make sure you don't have too many MediaPlayer objects active at once. See this for reference.

E/MediaPlayer: error (1, -19), E/MediaPlayer: error (0, -38)

swooby,

  1. Instantiate the Media player

    private static MediaPlayer mediaPlayer;
    public static MediaPlayer getMediaPlayer() {
    return mediaPlayer;
  2. It's best to place the code in a try/catch block to handle any exceptions that occur

    try {
    if (btn == firstBird){
    stopPlaying();
    mediaPlayer = MediaPlayer.create(getActivity()
    .getApplicationContext(),R.raw.bird_one);
    mediaPlayer.start();
    mediaPlayer.setLooping(true);
    timerTextView.setText("15:00");
    catch (IllegalStateException e){
    stopPlaying();
    }
  3. Create extra methods to prevent rewriting code

  4. A "stopPlaying()" method is helpful to stop and release the media player.
    This method should be called to ensure the media player is not previously engaged or playing from another activity, fragment, or current activity/fragment.

    public void stopPlaying() {
    if (mediaPlayer != null){
    mediaPlayer.stop();
    mediaPlayer.release();
    mediaPlayer = null;
  5. Call the stopPlaying() method before you begin a new mediaPlayer instance. See #2.
  6. If you are looping the sound within a countdown timer, be sure to use a method like this...

    @Override
    public void onFinish() {
    resetTimer();
    stopPlaying();
    }
  7. Lastly include an onCompletion() method within your "stop button" code

    if (btn == stopPlay){
    onCompletion(mediaPlayer);
    }
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
    mediaPlayer.stop();
    mediaPlayer.release();
    }

    This way the media player is stopped and released for the next operation.

Mediaplayer error (-38,0) and error (1,-1010)

I fixed it by changing a part in the onResumse(),

As expected it was indeed the permission causing havoc,
i had:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
if (!mPlayer.isPlaying() && !mpCreated) {
initTunnelPlayerWorkaround();
init();
} else {
mPlayer.start();
mVisualizerView.link(mPlayer);
}
} else {
cleanUp();
mPlayer.start();
Log.i("boop","biep");
}

And finally discovered by that log it was going into the else, meaning it was calling the cleanUp(); method

And in the cleanUp() method i had:

private void cleanUp() {
if (mPlayer != null) {
mVisualizerView.release();
if(!mPlayer.isPlaying()) {
mPlayer.pause();
}
}
}

The 2nd if was causing havoc (duh) and i fixed it by changing it to:

private void cleanUp() {
if (mPlayer != null) {
mVisualizerView.release();
if(mPlayer.isPlaying()) {
mPlayer.pause();
}
}

I know this is a weird and specific answer, but maybe someone else who had the same struggle can find out why he or she was having problems.

Have a great day all and thanks for trying to help

~Waro (dh19, couldnt use waro sadly)



Related Topics



Leave a reply



Submit