I would like to set delay time in milliseconds for the class which extends AsyncTask. But this class is using also another java Thread. There are classes to stream Mjpeg on Android with some few changes from this question: Android ICS and MJPEG using AsyncTask
This is my class which Extends AsyncTask:
public class GetCameraMjpeg extends AsyncTask<String, Void, MjpegInputStream> {
@Override
protected MjpegInputStream doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (username != null && password != null) {
String authEncoded = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
connection.setRequestProperty("Authorization", "Basic " + authEncoded);
}
connection.connect();
return new MjpegInputStream(connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(MjpegInputStream result) {
mjpegView.setSource(result);
mjpegView.setDisplayMode(MjpegView.SIZE_FULLSCREEN);
}
}
MjpegInputStream
class is the same as in the Stackoverflow question pasted above. And the MjpegView
is the class extending SurfaceView. It contains inner class MjpegViewThread
which is the class extending java Thread
class. And this is the run
method of this thread.
@Override
public void run() {
Bitmap bitmap;
Rect destRect;
Canvas canvas = null;
while (mjpegRun) {
if (surfaceDone) {
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
synchronized (surfaceHolder) {
try {
bitmap = mjpegInputStream.readMjpegFrame();
if (displayMode != SIZE_FULLSCREEN) {
displayWidth = bitmap.getWidth();
displayHeight = bitmap.getHeight();
}
destRect = destRect(bitmap.getWidth(), bitmap.getHeight());
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(bitmap, null, destRect, new Paint());
} catch (IOException e) {
e.getStackTrace();
Log.d(TAG, "catch IOException hit in run", e);
}
}
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
I have tried with this answer: How to execute Async task repeatedly after fixed time intervals but there is not noticeable difference. I suppose it is that MjpegView
runs its own java Thread
.