Skip to content

Commit 85d9ae5

Browse files
author
MaiKambayashi
committed
add trancode cancel method and refactoring example activity
1 parent 1952254 commit 85d9ae5

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

example/src/main/java/net/ypresto/androidtranscoder/example/TranscoderActivity.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class TranscoderActivity extends Activity {
2727
private static final String TAG = "TranscoderActivity";
2828
private static final int REQUEST_CODE_PICK = 1;
29+
private static final int PROGRESS_BAR_MAX = 1000;
2930

3031
@Override
3132
protected void onCreate(Bundle savedInstanceState) {
@@ -37,6 +38,12 @@ public void onClick(View v) {
3738
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("video/*"), REQUEST_CODE_PICK);
3839
}
3940
});
41+
findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View view) {
44+
MediaTranscoder.getInstance().cancel();
45+
}
46+
});
4047
}
4148

4249
@Override
@@ -63,7 +70,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
6370
}
6471
final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
6572
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
66-
progressBar.setMax(1000);
73+
progressBar.setMax(PROGRESS_BAR_MAX);
6774
final long startTime = SystemClock.uptimeMillis();
6875
MediaTranscoder.Listener listener = new MediaTranscoder.Listener() {
6976
@Override
@@ -72,42 +79,31 @@ public void onTranscodeProgress(double progress) {
7279
progressBar.setIndeterminate(true);
7380
} else {
7481
progressBar.setIndeterminate(false);
75-
progressBar.setProgress((int) Math.round(progress * 1000));
82+
progressBar.setProgress((int) Math.round(progress * PROGRESS_BAR_MAX));
7683
}
7784
}
7885

7986
@Override
8087
public void onTranscodeCompleted() {
8188
Log.d(TAG, "transcoding took " + (SystemClock.uptimeMillis() - startTime) + "ms");
82-
Toast.makeText(TranscoderActivity.this, "transcoded file placed on " + file, Toast.LENGTH_LONG).show();
83-
findViewById(R.id.select_video_button).setEnabled(true);
84-
progressBar.setIndeterminate(false);
85-
progressBar.setProgress(1000);
89+
transcodefinish(progressBar, PROGRESS_BAR_MAX, "transcoded file placed on " + file, parcelFileDescriptor);
8690
startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(file), "video/mp4"));
87-
try {
88-
parcelFileDescriptor.close();
89-
} catch (IOException e) {
90-
Log.w("Error while closing", e);
91-
}
91+
}
92+
93+
@Override
94+
public void onTranscodeCanceled() {
95+
transcodefinish(progressBar, 0, "Transcoder canceled.", parcelFileDescriptor);
9296
}
9397

9498
@Override
9599
public void onTranscodeFailed(Exception exception) {
96-
progressBar.setIndeterminate(false);
97-
progressBar.setProgress(0);
98-
findViewById(R.id.select_video_button).setEnabled(true);
99-
Toast.makeText(TranscoderActivity.this, "Transcoder error occurred.", Toast.LENGTH_LONG).show();
100-
try {
101-
parcelFileDescriptor.close();
102-
} catch (IOException e) {
103-
Log.w("Error while closing", e);
104-
}
100+
transcodefinish(progressBar, 0, "Transcoder error occurred.", parcelFileDescriptor);
105101
}
106102
};
107103
Log.d(TAG, "transcoding into " + file);
108104
MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(),
109105
MediaFormatStrategyPresets.createAndroid720pStrategy(), listener);
110-
findViewById(R.id.select_video_button).setEnabled(false);
106+
switchButtonEnabled(true);
111107
}
112108
break;
113109
}
@@ -134,4 +130,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
134130
}
135131
return super.onOptionsItemSelected(item);
136132
}
133+
134+
private void transcodefinish(ProgressBar progressBar, int progress, String toastMessage, ParcelFileDescriptor parcelFileDescriptor) {
135+
progressBar.setIndeterminate(false);
136+
progressBar.setProgress(progress);
137+
switchButtonEnabled(false);
138+
Toast.makeText(TranscoderActivity.this, toastMessage, Toast.LENGTH_LONG).show();
139+
try {
140+
parcelFileDescriptor.close();
141+
} catch (IOException e) {
142+
Log.w("Error while closing", e);
143+
}
144+
}
145+
146+
private void switchButtonEnabled(boolean isProgress) {
147+
findViewById(R.id.select_video_button).setEnabled(!isProgress);
148+
findViewById(R.id.cancel_button).setEnabled(isProgress);
149+
}
137150
}

example/src/main/res/layout/activity_transcoder.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@
3030
android:layout_gravity="center_horizontal"
3131
android:progress="0" />
3232

33+
<Button
34+
android:id="@+id/cancel_button"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:layout_gravity="center_horizontal"
38+
android:enabled="false"
39+
android:text="cancel" />
40+
3341
</LinearLayout>

lib/src/main/java/net/ypresto/androidtranscoder/MediaTranscoder.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.io.FileDescriptor;
2828
import java.io.FileInputStream;
2929
import java.io.IOException;
30+
import java.util.List;
31+
import java.util.concurrent.Executors;
3032
import java.util.concurrent.LinkedBlockingQueue;
3133
import java.util.concurrent.ThreadFactory;
3234
import java.util.concurrent.ThreadPoolExecutor;
@@ -124,6 +126,12 @@ public void onTranscodeCompleted() {
124126
closeStream();
125127
}
126128

129+
@Override
130+
public void onTranscodeCanceled() {
131+
listener.onTranscodeCanceled();
132+
closeStream();
133+
}
134+
127135
@Override
128136
public void onTranscodeFailed(Exception exception) {
129137
listener.onTranscodeFailed(exception);
@@ -153,6 +161,7 @@ public void transcodeVideo(final FileDescriptor inFileDescriptor, final String o
153161
Looper looper = Looper.myLooper();
154162
if (looper == null) looper = Looper.getMainLooper();
155163
final Handler handler = new Handler(looper);
164+
if (mExecutor.isShutdown()) mExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
156165
mExecutor.execute(new Runnable() {
157166
@Override
158167
public void run() {
@@ -177,7 +186,9 @@ public void run() {
177186
+ " or could not open output file ('" + outPath + "') .", e);
178187
caughtException = e;
179188
} catch (RuntimeException e) {
180-
Log.e(TAG, "Fatal error while transcoding, this might be invalid format or bug in engine or Android.", e);
189+
if (!(e.getCause() instanceof InterruptedException)) {
190+
Log.e(TAG, "Fatal error while transcoding, this might be invalid format or bug in engine or Android.", e);
191+
}
181192
caughtException = e;
182193
}
183194

@@ -188,14 +199,22 @@ public void run() {
188199
if (exception == null) {
189200
listener.onTranscodeCompleted();
190201
} else {
191-
listener.onTranscodeFailed(exception);
202+
if (exception.getCause() instanceof InterruptedException) {
203+
listener.onTranscodeCanceled();
204+
} else {
205+
listener.onTranscodeFailed(exception);
206+
}
192207
}
193208
}
194209
});
195210
}
196211
});
197212
}
198213

214+
public List<Runnable> cancel() {
215+
return mExecutor.shutdownNow();
216+
}
217+
199218
public interface Listener {
200219
/**
201220
* Called to notify progress.
@@ -209,6 +228,11 @@ public interface Listener {
209228
*/
210229
void onTranscodeCompleted();
211230

231+
/**
232+
* Called when transcode canceled.
233+
*/
234+
void onTranscodeCanceled();
235+
212236
/**
213237
* Called when transcode failed.
214238
*

0 commit comments

Comments
 (0)