0

I have a function name CreateVideo(), the problems are:

  1. Because function must be run until it finishs to create a video, I cannot know how many percent completed of the video.
  2. Did I put the function at a right place?

Here is the code:

private class MyVideo extends AsyncTask<Void, Void, String> {

    private Context context;
    private String output;
    private String quality;
    private FramePackage framePackage;

    CreateMP4Video createMP4Video;

    public MyVideo(Context context, FramePackage framePackage, String output, String quality) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.output = output;
        this.quality = quality;
        this.framePackage = framePackage;
    }

    @Override 
    protected void onPreExecute() {
        super.onPreExecute();
        // Create Video Maker
        createMP4Video = new CreateMP4Video(context, framePackage, output, quality);

        // Create progress
        mProgress = new ProgressDialog(MainActivity.this);
        mProgress.setTitle("Please Wait..");
        mProgress.setMessage("Creating Video...");
        mProgress.setProgressStyle(mProgress.STYLE_HORIZONTAL);
        mProgress.setProgress(0);

        // set Max = total number of frame
        mProgress.setMax(framePackage.getCount());
        mProgress.setCancelable(false);
        mProgress.show();
    };


    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            createMP4Video.CreateVideo();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (mProgress.getProgress() <= mProgress.getMax()) {

            try {

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            updateBarHandler.post(new Runnable() {

                public void run() {
                    // get the current frame (frame 1, 2, 3,...)
                    // that already added to video
                    // In thist case, it always get the last frame
                    // progress bar run from 0% to 100% directly
                    mProgress.setProgress(createMP4Video.getCurrentFrame());
                  }
              });

            if (mProgress.getProgress() == mProgress.getMax()) {    
                mProgress.dismiss();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
    }

}

Thank you in advance!

Luong Truong
  • 1,953
  • 2
  • 27
  • 46

1 Answers1

0

Put theprogress dialog in onPreExecute() method as u have done

// Create progress

mProgress = new ProgressDialog(MainActivity.this);
        mProgress.setTitle("Please Wait..");
        mProgress.setMessage("Creating Video...");
        mProgress.setProgressStyle(mProgress.STYLE_HORIZONTAL);
        mProgress.setProgress(0);

        // set Max = total number of frame
        mProgress.setMax(framePackage.getCount());
        mProgress.setCancelable(false);
        mProgress.show();

Dismiss the dialog in onPostExecute

You can take a look at the progressupdate example ONPROGRESS UPDATE

Community
  • 1
  • 1
George Thomas
  • 4,566
  • 5
  • 30
  • 65
  • Thank you, I did it, but the problem is that I cannot handle the progress bar to show the percentage. For example: 0% -> 20% -> 40% -> 60% -> 80% -> 100%. I call the function CreateVideo(), it will run until it finish, so I can catch the percentage. Do you have any ideal to handle it? – Luong Truong Jul 11 '14 at 04:06
  • 2
    @user3675966 did you read asynctask docs in the first place. Read about onProgressUpdate – Raghunandan Jul 11 '14 at 04:15
  • 1
    if you need the progress values you can overide another function in AsyncTask i.e OnprogressUpdate use this method to show the progress in the UI – George Thomas Jul 11 '14 at 04:17
  • I just find out that onProgressUpdate can do it. I am researching about it. Do you know any documents which are written for beginners? Thank you in advance! – Luong Truong Jul 11 '14 at 04:33
  • You can check the link in my answer or check this link http://www.mkyong.com/android/android-progress-bar-example/ – George Thomas Jul 11 '14 at 04:57