Search code examples
androidsocketsandroid-asynctasklive-streamingasync-onprogressupdate

UDP Image streaming Android


I want to send frames, captured from a webcam, to my android application over UDP Sockets, and display them so that it appears as live-stream. I am able to receive images at the Android App, but I can only display single image. Not a stream. What I have done is following.

public class getImage extends AsyncTask<String, Bitmap, Bitmap> {
protected void onProgressUpdate(Bitmap... values) {
    Log.i("onProgressUpdate", "done");
    imageDisplay.setImageBitmap(values[0]); //imageDisplay is an ImageView on UI
    imageDisplay.invalidate();
}//end of onProgressUpdate


protected Bitmap doInBackground(String... params) {
//receive an image over socket
publishProgress(bitmapimage); //update on UI
return bitmapimage;

}//end of getImage


public void ConnectionButton(View v) {
Connect = (Button) findViewById(R.id.Connect);
//while(true) {
     try {
          clientsocket = new DatagramSocket(9999);

        } catch (IOException e) {
          Log.i("Connection", "exception while creating socket");
        }

     try {
          getImage obj = new getImage();
          receivedImage = obj.execute("9999").get(); //receive image and over stream
          //obj.onProgressUpdate(receivedImage);

       }
   catch(Exception e){
   e.printStackTrace();
}
    //}//endof while
}//end of ConnectionButton

If I press, the Connect button repeatedly, I get images as stream. I have tried onProgress update, but it updates images infrequently with quite large delay. How can I make these received images display on application as real-time stream.


Solution

  • An AsyncTaskobject can be executed only once, so I was unable to receive a stream. But I achieved it through following mehtod.

    I overrode onPostExecute() to return a single image received from socket. Removed my call to publishProgress() in doInBackground(). And, the last step was to re-initialize the class object which extended AsyncTask and execute it in the onPostExecute() i.e. create a new object and asyncobject.execute().get() it in the onPostExecute(). Hope it helps, anyone else facing the same issue.