27
27
import java .io .FileDescriptor ;
28
28
import java .io .FileInputStream ;
29
29
import java .io .IOException ;
30
- import java .util .List ;
31
- import java .util .concurrent .Executors ;
30
+ import java .util .concurrent .Future ;
32
31
import java .util .concurrent .LinkedBlockingQueue ;
33
32
import java .util .concurrent .ThreadFactory ;
34
33
import java .util .concurrent .ThreadPoolExecutor ;
@@ -38,9 +37,11 @@ public class MediaTranscoder {
38
37
private static final String TAG = "MediaTranscoder" ;
39
38
private static final int MAXIMUM_THREAD = 1 ; // TODO
40
39
private static volatile MediaTranscoder sMediaTranscoder ;
40
+ private Future mFuture ;
41
41
private ThreadPoolExecutor mExecutor ;
42
42
43
43
private MediaTranscoder () {
44
+ mFuture = null ;
44
45
mExecutor = new ThreadPoolExecutor (
45
46
0 , MAXIMUM_THREAD , 60 , TimeUnit .SECONDS ,
46
47
new LinkedBlockingQueue <Runnable >(),
@@ -161,8 +162,7 @@ public void transcodeVideo(final FileDescriptor inFileDescriptor, final String o
161
162
Looper looper = Looper .myLooper ();
162
163
if (looper == null ) looper = Looper .getMainLooper ();
163
164
final Handler handler = new Handler (looper );
164
- if (mExecutor .isShutdown ()) mExecutor = (ThreadPoolExecutor ) Executors .newCachedThreadPool ();
165
- mExecutor .execute (new Runnable () {
165
+ mFuture = mExecutor .submit (new Runnable () {
166
166
@ Override
167
167
public void run () {
168
168
Exception caughtException = null ;
@@ -185,10 +185,11 @@ public void run() {
185
185
Log .w (TAG , "Transcode failed: input file (fd: " + inFileDescriptor .toString () + ") not found"
186
186
+ " or could not open output file ('" + outPath + "') ." , e );
187
187
caughtException = e ;
188
+ } catch (InterruptedException e ) {
189
+ Log .i (TAG , "Cancel transcode video file." , e );
190
+ caughtException = e ;
188
191
} catch (RuntimeException 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
- }
192
+ Log .e (TAG , "Fatal error while transcoding, this might be invalid format or bug in engine or Android." , e );
192
193
caughtException = e ;
193
194
}
194
195
@@ -199,7 +200,7 @@ public void run() {
199
200
if (exception == null ) {
200
201
listener .onTranscodeCompleted ();
201
202
} else {
202
- if (exception . getCause () instanceof InterruptedException ) {
203
+ if (exception instanceof InterruptedException ) {
203
204
listener .onTranscodeCanceled ();
204
205
} else {
205
206
listener .onTranscodeFailed (exception );
@@ -211,8 +212,11 @@ public void run() {
211
212
});
212
213
}
213
214
214
- public List <Runnable > cancel () {
215
- return mExecutor .shutdownNow ();
215
+ /**
216
+ * Cancel transcode video file
217
+ */
218
+ public boolean cancel () {
219
+ return mFuture != null ? mFuture .cancel (true ) : false ;
216
220
}
217
221
218
222
public interface Listener {
0 commit comments