@@ -316,48 +316,15 @@ public void cropVideo(InstagramMediaProcessActivity activity, boolean isAspectRa
316316 } else {
317317 builder .addDataSource (new ClipDataSource (new FilePathDataSource (mMedia .getPath ()), startTimeUS , endTimeUS ));
318318 }
319- mTranscodeFuture = builder .setListener (new TranscoderListener () {
320- @ Override
321- public void onTranscodeProgress (double progress ) {
322- if (mLoadingDialog != null
323- && mLoadingDialog .isShowing ()) {
324- mLoadingDialog .updateProgress (progress );
325- }
326- }
327-
328- @ Override
329- public void onTranscodeCompleted (int successCode ) {
330- if (successCode == Transcoder .SUCCESS_TRANSCODED ) {
331- mMedia .setDuration (endTime - startTime );
332- mMedia .setPath (transcodeOutputFile .getAbsolutePath ());
333- mMedia .setAndroidQToPath (SdkVersionUtils .checkedAndroid_Q () ? transcodeOutputFile .getAbsolutePath () : mMedia .getAndroidQToPath ());
334- List <LocalMedia > list = new ArrayList <>();
335- list .add (mMedia );
336- activity .setResult (Activity .RESULT_OK , new Intent ().putParcelableArrayListExtra (PictureConfig .EXTRA_SELECT_LIST , (ArrayList <? extends Parcelable >) list ));
337- activity .finish ();
338- } else if (successCode == Transcoder .SUCCESS_NOT_NEEDED ) {
339-
340- }
341- showLoadingView (false );
342- }
343-
344- @ Override
345- public void onTranscodeCanceled () {
346- showLoadingView (false );
347- }
348-
349- @ Override
350- public void onTranscodeFailed (@ NonNull Throwable exception ) {
351- exception .printStackTrace ();
352- ToastUtils .s (getContext (), getContext ().getString (R .string .video_clip_failed ));
353- showLoadingView (false );
354- }
355- })
319+ mTranscodeFuture = builder .setListener (new TranscoderListenerImpl (this , activity , startTime , endTime , transcodeOutputFile ))
356320 .setVideoTrackStrategy (videoStrategy )
357321 .transcode ();
358322 }
359323
360324 private void showLoadingView (boolean isShow ) {
325+ if (((Activity ) getContext ()).isFinishing ()) {
326+ return ;
327+ }
361328 if (isShow ) {
362329 if (mLoadingDialog == null ) {
363330 mLoadingDialog = new InstagramLoadingDialog (getContext ());
@@ -524,6 +491,10 @@ public void onDestroy() {
524491 mFrameTask .cancel (true );
525492 mFrameTask = null ;
526493 }
494+ if (mTranscodeFuture != null ) {
495+ mTranscodeFuture .cancel (true );
496+ mTranscodeFuture = null ;
497+ }
527498 }
528499
529500 public static class OnSingleBitmapListenerImpl implements getAllFrameTask .OnSingleBitmapListener {
@@ -562,6 +533,76 @@ public void run() {
562533
563534 public interface VideoPauseListener {
564535 void onChange ();
536+
565537 void onVideoPause ();
566538 }
539+
540+ private static class TranscoderListenerImpl implements TranscoderListener {
541+ private WeakReference <TrimContainer > mContainerWeakReference ;
542+ private WeakReference <InstagramMediaProcessActivity > mActivityWeakReference ;
543+ private long mStartTime ;
544+ private long mEndTime ;
545+ private File mTranscodeOutputFile ;
546+
547+ public TranscoderListenerImpl (TrimContainer container , InstagramMediaProcessActivity activity , long startTime , long endTime , File transcodeOutputFile ) {
548+ mContainerWeakReference = new WeakReference <>(container );
549+ mActivityWeakReference = new WeakReference <>(activity );
550+ mStartTime = startTime ;
551+ mEndTime = endTime ;
552+ mTranscodeOutputFile = transcodeOutputFile ;
553+ }
554+
555+ @ Override
556+ public void onTranscodeProgress (double progress ) {
557+ TrimContainer trimContainer = mContainerWeakReference .get ();
558+ if (trimContainer == null ) {
559+ return ;
560+ }
561+ if (trimContainer .mLoadingDialog != null
562+ && trimContainer .mLoadingDialog .isShowing ()) {
563+ trimContainer .mLoadingDialog .updateProgress (progress );
564+ }
565+ }
566+
567+ @ Override
568+ public void onTranscodeCompleted (int successCode ) {
569+ TrimContainer trimContainer = mContainerWeakReference .get ();
570+ InstagramMediaProcessActivity activity = mActivityWeakReference .get ();
571+ if (trimContainer == null || activity == null ) {
572+ return ;
573+ }
574+ if (successCode == Transcoder .SUCCESS_TRANSCODED ) {
575+ trimContainer .mMedia .setDuration (mEndTime - mStartTime );
576+ trimContainer .mMedia .setPath (mTranscodeOutputFile .getAbsolutePath ());
577+ trimContainer .mMedia .setAndroidQToPath (SdkVersionUtils .checkedAndroid_Q () ? mTranscodeOutputFile .getAbsolutePath () : trimContainer .mMedia .getAndroidQToPath ());
578+ List <LocalMedia > list = new ArrayList <>();
579+ list .add (trimContainer .mMedia );
580+ activity .setResult (Activity .RESULT_OK , new Intent ().putParcelableArrayListExtra (PictureConfig .EXTRA_SELECT_LIST , (ArrayList <? extends Parcelable >) list ));
581+ activity .finish ();
582+ } else if (successCode == Transcoder .SUCCESS_NOT_NEEDED ) {
583+
584+ }
585+ trimContainer .showLoadingView (false );
586+ }
587+
588+ @ Override
589+ public void onTranscodeCanceled () {
590+ TrimContainer trimContainer = mContainerWeakReference .get ();
591+ if (trimContainer == null ) {
592+ return ;
593+ }
594+ trimContainer .showLoadingView (false );
595+ }
596+
597+ @ Override
598+ public void onTranscodeFailed (@ NonNull Throwable exception ) {
599+ TrimContainer trimContainer = mContainerWeakReference .get ();
600+ if (trimContainer == null ) {
601+ return ;
602+ }
603+ exception .printStackTrace ();
604+ ToastUtils .s (trimContainer .getContext (), trimContainer .getContext ().getString (R .string .video_clip_failed ));
605+ trimContainer .showLoadingView (false );
606+ }
607+ }
567608}
0 commit comments