0

I am writing an app that streams videos from the network using MediaPlayer and SurfaceView. All is working fine most of the time but I've noticed that after some repeated app restarts and debugging I am not able to stream anymore files.

I close and restart the app, rebuild and run, closing and uninstalling it from the device.. nothing helps. When the issue happens I am getting this error from the MediaPlayer (Unknown error):

E/MediaPlayer﹕ error (1, -2147483648)

The only thing that let me return to normal behaviour so that everything is working and the streaming is good is restarting the device. No code change and no other steps, just restart and re-run the app (not ever re-installing it..)

Creating the MediaPlayer object as follow:

   mMediaPlayer = new MediaPlayer();
   mMediaPlayer.setDisplay(mSurfaceHolder);
   mMediaPlayer.setDataSource(getContext(), mVideoUrl);
   mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   mMediaPlayer.setLooping(mRunLopping);
   mMediaPlayer.setScreenOnWhilePlaying(true);
   mMediaPlayer.setOnPreparedListener(this);
   mMediaPlayer.setOnErrorListener(this);
   mMediaPlayer.prepareAsync();

Maybe something with global device resources? caching? any clue will help.

Idan
  • 2,819
  • 4
  • 33
  • 61
  • How are you creating the MediaPlayer object(s)? – summea Jan 07 '15 at 19:45
  • 1
    updated my question with the code – Idan Jan 07 '15 at 19:49
  • Thanks for adding that code; it may be a different issue entirely, but I only ask because I recently ran up against an issue with MediaPlayer where after a certain number of times of creating new instances of a MediaPlayer object, media would no longer play back. It may not be the best way to fix the issue, but I ended up using a static MediaPlayer object so that I would only have one instance in use for my entire app... Also, the _Related_ questions on the bottom right side of this page might have some sort of hint as to why this is happening _(unless you have already checked all of those!)_ – summea Jan 07 '15 at 19:56
  • Single instance is not an option for my app as I play several media files simultaneously. The question in the related section might help if it was answered.. – Idan Jan 07 '15 at 20:14
  • Even [this question](http://stackoverflow.com/q/18833537/1167750)? – summea Jan 07 '15 at 20:20
  • Yes. It's not that I'm getting the error all the time.. The code works on several devices and the emulator. The media comes streaming and not local and the problem occurs only after some time of repeated app activations. – Idan Jan 07 '15 at 20:24
  • How many media files do you play simultaneously? – summea Jan 08 '15 at 19:18
  • Even though I may not agree with the design decision(s) of playing 2-4 media files at the same time for a single user, does this error still happen even if you try creating 2-4 different, static MediaPlayer objects? – summea Jan 08 '15 at 22:57

1 Answers1

0

This is how I do it, each time the MediaPlayer is called...not sure if it's the correct way but I had the same thing happening and this helped. Looks like your code might be stacking MediaPlayer instances on top of each other.

public static MediaPlayer mPlayer = null;

void createMediaPlayerIfNeeded() {
if (mPlayer == null) {
    mPlayer = new MediaPlayer();

    mPlayer.setOnPreparedListener(this);
    mPlayer.setOnCompletionListener(this);
    mPlayer.setOnErrorListener(this);
} else
    mPlayer.reset();
}
}

Stopping the MediaPLayer:

if (mPlayer != null) {
    mPlayer.reset();
    mPlayer.release();
    mPlayer = null;
}
midiwriter
  • 426
  • 5
  • 12
  • My MediaPlayer instance is inside a custom control that dies when the activity is closed. My issue happens even if I close the app entirely... (Force close button in app settings). – Idan Jan 07 '15 at 21:59