Im using a SurfaceView
and MediaPlayer
to stream video from RTSP
and MJPEG both protocols work independently that is when I stream from RTSP
only or MJPEG over HTTP
only. The problem Im having is when I try to switch from MJPEG protocol to the other thats when the prepare()
method throws an IllegalStateException
.
For RTSP streaming I use the MediaPlayer
class since it supports RTSP
streaming by default. For MJPEG
I have an AsyncTask that calls an HTTP url and returns the JPEG
which I use to set draw on the SurfaceView
s SurfaceHolder
Canvas
. I think that the problem is somewhere when Im trying to unlock the SurfaceHolder
Canvas
The first step when I try to change the stream from MJPEG to RTSP:
MjpegThread.isRunning = false;
mediaPlayer.release();
mediaPlayer = null;
setMediaPlayer();
The MjpegThread
is the AsyncTask
that makes HTTP
requests and updates the SurfaceHolder
s Canvas
after stopping the AsyncTask
at the end of the doInBackground
method I call
surfaceHolder.unlockCanvasAndPost(canvas);
The last step in the process is displaying the RTSP stream heres my code:
mediaPlayer.setDisplay(vidHolder);
mediaPlayer.setDataSource(OZOptions.RTSP_URL);
mediaPlayer.prepare(); // <- IllegalStateException HERE
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
The code above works when I initially use RTSP
bot not when I switch from MJPEG
to RTSP
.