Search code examples
javaandroidandroid-asynctaskjava-threads

Too much work in main thread issue warning.How add AsyncTask?


Guys help add asynctask to my app.I have issues with adding this.Maybe any hints how realize this.Its rtsp video player.App communicate with web server and drawing video using canvas.Async task help to fix issue with main thread and skippedframes?

if (frameNum >= skipNum) {
  //Log.d(TAG, String.format("onFrame: nChannel = %d, width = %d, height = %d", nChannel, width, height));
  try {
    Canvas canvas = surface.lockCanvas(null);

      try {

        int area = width * height;
        int pixels[] = new int[area];

        for (int i = 0; i < area; i++) {
          int r = frame[3 * i];
          int g = frame[3 * i + 1];
          int b = frame[3 * i + 2];
          if (r < 0) r += 255;
          if (g < 0) g += 255;
          if (b < 0) b += 255;
          pixels[i] = Color.rgb(r, g, b);
        }

        Bitmap bmp = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
        canvas.drawBitmap(bmp, null, videoRect, null);

      } finally {
        surface.unlockCanvasAndPost(canvas);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    frameNum = 0;
  }
}

Solution

  • Not sure of you Android API level however Async Task is now deprecated.

    This class was deprecated in API level 30. Use the standard java.util.concurrent or Kotlin concurrency utilities instead.

    As your code is Java here is the link to Concurrent : https://developer.android.com/reference/java/util/concurrent/package-summary