Skip to content

Commit

Permalink
[new] 增加取消压缩方法 VideoCompress.compressCancel();
Browse files Browse the repository at this point in the history
  • Loading branch information
qqnp1100 committed Jul 27, 2019
1 parent 8b5e1ed commit 9d6511b
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 123 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
...
})
```
- 增加取消压缩方法
```
VideoCompress.compressCancel();
```
# 本分支集成方式

Step 1. Add the JitPack repository to your build file
Expand Down
212 changes: 110 additions & 102 deletions app/src/main/java/com/vincent/videocompress/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Bundle;
Expand All @@ -16,6 +17,7 @@

import com.vincent.videocompressor.CompressConfig;
import com.vincent.videocompressor.VideoCompress;
import com.vincent.videocompressor.VideoController;

import java.io.File;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -49,113 +51,119 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) {

private void initView() {
Button btn_select = (Button) findViewById(R.id.btn_select);
btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
//intent.setType("video/*;image/*");
//intent.setType("audio/*"); //选择音频
intent.setType("video/*"); //选择视频 (mp4 3gp 是android支持的视频格式)
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_FOR_VIDEO_FILE);
}
btn_select.setOnClickListener(view -> {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
//intent.setType("video/*;image/*");
//intent.setType("audio/*"); //选择音频
intent.setType("video/*"); //选择视频 (mp4 3gp 是android支持的视频格式)
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_FOR_VIDEO_FILE);
});

Button btn_compress = (Button) findViewById(R.id.btn_compress);
btn_compress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String destPath = tv_output.getText().toString() + File.separator + "VID_" + new SimpleDateFormat("yyyyMMdd_HHmmss", getLocale()).format(new Date()) + ".mp4";
VideoCompress.compressVideoLow(tv_input.getText().toString(), destPath, new VideoCompress.CompressListener() {
@Override
public void onStart() {
tv_indicator.setText("Compressing..." + "\n"
+ "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
}

@Override
public void onSuccess() {
String previous = tv_indicator.getText().toString();
tv_indicator.setText(previous + "\n"
+ "Compress Success!" + "\n"
+ "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
Util.writeFile(MainActivity.this, "Total: " + ((endTime - startTime) / 1000) + "s" + "\n");
Util.writeFile(MainActivity.this);
}

@Override
public void onFail() {
tv_indicator.setText("Compress Failed!");
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Failed Compress!!!" + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
}

@Override
public void onProgress(float percent) {
tv_progress.setText(String.valueOf(percent) + "%");
}
});
}
btn_compress.setOnClickListener(view -> {
String destPath = tv_output.getText().toString() + File.separator + "VID_" + new SimpleDateFormat("yyyyMMdd_HHmmss", getLocale()).format(new Date()) + ".mp4";
VideoCompress.compressVideoLow(tv_input.getText().toString(), destPath, new VideoCompress.CompressListener() {
@Override
public void onCancel() {
pb_compress.setVisibility(View.INVISIBLE);
tv_indicator.setText("Compress Cancel!");
}

@Override
public void onStart() {
tv_indicator.setText("Compressing..." + "\n"
+ "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
}

@Override
public void onSuccess() {
String previous = tv_indicator.getText().toString();
tv_indicator.setText(previous + "\n"
+ "Compress Success!" + "\n"
+ "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
Util.writeFile(MainActivity.this, "Total: " + ((endTime - startTime) / 1000) + "s" + "\n");
Util.writeFile(MainActivity.this);
}

@Override
public void onFail() {
tv_indicator.setText("Compress Failed!");
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Failed Compress!!!" + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
}

@Override
public void onProgress(float percent) {
tv_progress.setText(String.valueOf(percent) + "%");
}
});
});

findViewById(R.id.btn_custom).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String srcPath = tv_input.getText().toString();
String destPath = tv_output.getText().toString() + File.separator + "VID_" + new SimpleDateFormat("yyyyMMdd_HHmmss", getLocale()).format(new Date()) + ".mp4";
CompressConfig config = new CompressConfig
.Builder()
.width(1280)
.height(720)
.bitrate(1280 * 720 * 2)
.frame(24)
.build();
VideoCompress.compressVideoCustom(srcPath, destPath, config, new VideoCompress.CompressListener() {
@Override
public void onStart() {
tv_indicator.setText("Compressing..." + "\n"
+ "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
}

@Override
public void onSuccess() {
String previous = tv_indicator.getText().toString();
tv_indicator.setText(previous + "\n"
+ "Compress Success!" + "\n"
+ "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
Util.writeFile(MainActivity.this, "Total: " + ((endTime - startTime) / 1000) + "s" + "\n");
Util.writeFile(MainActivity.this);
}

@Override
public void onFail() {
tv_indicator.setText("Compress Failed!");
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Failed Compress!!!" + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
}

@Override
public void onProgress(float percent) {
tv_progress.setText(String.valueOf(percent) + "%");
}
});
}
findViewById(R.id.btn_custom).setOnClickListener(v -> {
String srcPath = tv_input.getText().toString();
String destPath = tv_output.getText().toString() + File.separator + "VID_" + new SimpleDateFormat("yyyyMMdd_HHmmss", getLocale()).format(new Date()) + ".mp4";
CompressConfig config = new CompressConfig
.Builder()
.width(1280)
.height(720)
.bitrate(1280 * 720 * 2)
.frame(24)
.build();
VideoCompress.compressVideoCustom(srcPath, destPath, config, new VideoCompress.CompressListener() {
@Override
public void onStart() {
tv_indicator.setText("Compressing..." + "\n"
+ "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Start at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
}

@Override
public void onSuccess() {
String previous = tv_indicator.getText().toString();
tv_indicator.setText(previous + "\n"
+ "Compress Success!" + "\n"
+ "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "End at: " + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()) + "\n");
Util.writeFile(MainActivity.this, "Total: " + ((endTime - startTime) / 1000) + "s" + "\n");
Util.writeFile(MainActivity.this);
}

@Override
public void onCancel() {
pb_compress.setVisibility(View.INVISIBLE);
tv_indicator.setText("Compress Cancel!");
}

@Override
public void onFail() {
tv_indicator.setText("Compress Failed!");
pb_compress.setVisibility(View.INVISIBLE);
endTime = System.currentTimeMillis();
Util.writeFile(MainActivity.this, "Failed Compress!!!" + new SimpleDateFormat("HH:mm:ss", getLocale()).format(new Date()));
}

@Override
public void onProgress(float percent) {
tv_progress.setText(String.valueOf(percent) + "%");
}
});
});

findViewById(R.id.btn_cancel).setOnClickListener(v -> {
VideoCompress.compressCancel();
});

tv_input = (TextView) findViewById(R.id.tv_input);
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
android:text="Select File"
android:textAllCaps="false" />

<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Compress"
android:textAllCaps="false" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">

<Button
android:id="@+id/btn_compress"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public static VideoCompressTask compressVideoCustom(String srcPath, String destP
return task;
}

private static class VideoCompressTask extends AsyncTask<String, Float, Boolean> {
public static void compressCancel() {
VideoController.getInstance().cancelVideoConvert();
}

private static class VideoCompressTask extends AsyncTask<String, Float, Integer> {
private CompressListener mListener;
private int mQuality;
private CompressConfig mConfig;
Expand All @@ -60,13 +64,8 @@ protected void onPreExecute() {
}

@Override
protected Boolean doInBackground(String... paths) {
return VideoController.getInstance().convertVideo(paths[0], paths[1], mQuality, mConfig, new VideoController.CompressProgressListener() {
@Override
public void onProgress(float percent) {
publishProgress(percent);
}
});
protected Integer doInBackground(String... paths) {
return VideoController.getInstance().convertVideo(paths[0], paths[1], mQuality, mConfig, percent -> publishProgress(percent));
}

@Override
Expand All @@ -78,13 +77,19 @@ protected void onProgressUpdate(Float... percent) {
}

@Override
protected void onPostExecute(Boolean result) {
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (mListener != null) {
if (result) {
mListener.onSuccess();
} else {
mListener.onFail();
switch (result) {
case VideoController.COMPRESS_TYPE_CANCEL:
mListener.onCancel();
break;
case VideoController.COMPRESS_TYPE_SUCCESS:
mListener.onSuccess();
break;
case VideoController.COMPRESS_TYPE_FAIL:
mListener.onFail();
break;
}
}
}
Expand All @@ -95,6 +100,8 @@ public interface CompressListener {

void onSuccess();

void onCancel();

void onFail();

void onProgress(float percent);
Expand Down
Loading

0 comments on commit 9d6511b

Please sign in to comment.