0
switch (view.getId()) {

      case R.id.trimmBttn:
        final Integer stime[] = { 2, 4, 7, 9, 12, 18 };
        final Integer endTime[] = { 4, 6, 9, 11, 15, 20 };

        Thread thread = new Thread(new Runnable() {
          @Override public void run() {
            for (int i = 0; i < stime.length; i++) {

              trimmVideo = new TrimmVideo(getExternalFilesDir(null) + "/12.mp4", stime[i],
                  endTime[i]).execute();
            }
          }
        });

        thread.start();

        try {
          thread.join();
        } catch (InterruptedException e) {

        }

        break;
    }




    private class TrimmVideo extends AsyncTask<Void, Void, Void> {
    private String mediaPath;
    private double startTime;
    private double endTime;
    private int length;
    private ProgressDialog progressDialog;

    private TrimmVideo(String mediaPath, int startTime, int length) {
      this.mediaPath = mediaPath;
      this.startTime = startTime;
      this.length = length;
      this.endTime = this.startTime + this.length;
    }

    @Override protected void onPreExecute() {
      progressDialog = ProgressDialog.show(context, "Trimming videos", "Please wait...", true);
      super.onPreExecute();
    }

    @Override protected Void doInBackground(Void... params) {
      trimVideo();
      return null;
    }

    @Override protected void onPostExecute(Void result) {
      progressDialog.dismiss();
      super.onPostExecute(result);
    }

This is my code i want to execute Asyck task synchronously inside for loop i.e when loop start from 0 then for 0 value its completed task then it should start for value next asynk task execution if 1 is complted then for 2 and so on ... please suggest me i am trying to using thread with join but still not working .

jdonmoyer
  • 1,245
  • 1
  • 18
  • 28
Adevelopment
  • 265
  • 3
  • 15

3 Answers3

0

you can use async task in serial order

new TrimmVideo(getExternalFilesDir(null) + "/12.mp4", stime[i],
              endTime[i]).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR)

like this.

may be help you.

Vij
  • 445
  • 2
  • 9
  • Ok Wait let me check this – Adevelopment Sep 22 '17 at 08:00
  • i used in my project it works inside loop it executes in serial order but above solution not work ,then you can make interface for task complete when first task complete then inside of OnPostExecute to call new second async task and so on. – Vij Sep 22 '17 at 08:09
0

Why don't you make new Async requests from onPostExecute()?

Alternate solution: How do I run Async tasks synchronously without post execute chaining?

Suhayl SH
  • 1,213
  • 1
  • 11
  • 16
  • coz we have not only two asynk task we have multiple asnyk task can u please suggest me how i will excute in on post excute with multiple time – Adevelopment Sep 22 '17 at 08:05
0

You can do something like:

 Integer[] stime;
 Integer[] endTime;

switch (view.getId()) {
  case R.id.trimmBttn:
     stime = new Integer[]{2, 4, 7, 9, 12, 18};
     endTime = new Integer[]{ 4, 6, 9, 11, 15, 20 };

    trimmVideo = new TrimmVideo().execute();

    break;
}

and use forloop in doInBackground() method like this

@Override
    protected Void doInBackground(Void... params) {
        for (int i = 0; i < stime.length; i++) {
        trimVideo(trimVideo(getExternalFilesDir(null) + "/12.mp4", stime[i],
              endTime[i]));
        }
        return null;
    }

You save memory by creating only one AsyncTask object instance

Geeta Gupta
  • 1,622
  • 11
  • 17