AbedElazizShe / LightCompressor

A powerful and easy-to-use video compression library for android uses MediaCodec API.
Apache License 2.0
529 stars 116 forks source link

How to cancel compression job correctly? #1

Closed huynguyennovem closed 4 years ago

huynguyennovem commented 4 years ago

I have tried to implement as below by getting a Job when starting a video compression, but this does not work. The job cancelation is executed but its task still runs (I was checking log to know that via Log.d(TAG, "Compress process: " + v); I think I need to get the correct scope of that compression coroutine so that I can cancel it.

// Compress
mCompressJob = VideoCompressor.INSTANCE.doVideoCompression(source.getPath(), mNewTempFileUpload.getPath(), new CompressionListener() {
@Override
public void onStart() {
    Log.d(TAG, "Compress starting...");
    mProgressDialog.show();
}

@Override
public void onSuccess() {
    Log.d(TAG, "Compress success");
    if (mProgressDialog != null) {
        mProgressDialog.dismiss();
    }
    checkUploadLimitSize(mNewTempFileUpload);
}

@Override
public void onFailure() {
    Log.d(TAG, "Compress fail");
    handleWhenCompressFail(false, source.getPath(), mNewTempFileUpload.getPath());
}

@Override
public void onProgress(float v) {
    Log.d(TAG, "Compress process: " + v);
    if (v > 0) {
        mProgressDialog.setProgress((int) v);
    }
}
});

// Cancel
if (mCompressJob != null && mCompressJob.isActive() && !mCompressJob.isCancelled()) {
    // cancel current running job
    CancellationException cancellationException = new CancellationException();
    cancellationException.printStackTrace();
    mCompressJob.cancel(cancellationException);
}
AbedElazizShe commented 4 years ago

Thank you for raising this issue, I will look into it in a couple of days :D

huynguyennovem commented 4 years ago

Thank you for raising this issue, I will look into it in a couple of days :D

Yeh, I think the problem comes from the running task (compression task). No matter how I cancel the coroutine job, the task is still running (I saw the while loop).

AbedElazizShe commented 4 years ago

I added a way to cancel the compression, just call VideoCompressor.cancel() and that would get the job done. Let me know if it works fine with you.

huynguyennovem commented 4 years ago

I added a way to cancel the compression, just call VideoCompressor.cancel() and that would get the job done. Let me know if it works fine with you.

Many thanks! This worked perfectly (like)(like)(like)(like)