11package protect .videotranscoder .activity ;
22
33import android .Manifest ;
4- import android .app .ProgressDialog ;
54import android .content .ContentUris ;
65import android .content .Context ;
76import android .content .DialogInterface ;
2726import android .widget .AdapterView ;
2827import android .widget .ArrayAdapter ;
2928import android .widget .Button ;
29+ import android .widget .ProgressBar ;
3030import android .widget .Spinner ;
3131import android .widget .TextView ;
3232import android .widget .Toast ;
@@ -96,7 +96,7 @@ public class MainActivity extends AppCompatActivity
9696 private VideoView videoView ;
9797 private CrystalRangeSeekbar rangeSeekBar ;
9898 private Timer videoTimer = null ;
99- private ProgressDialog progressDialog ;
99+ private ProgressBar progressBar ;
100100
101101 private Spinner containerSpinner ;
102102 private Spinner videoCodecSpinner ;
@@ -109,7 +109,9 @@ public class MainActivity extends AppCompatActivity
109109 private Spinner audioChannelSpinner ;
110110
111111 private TextView tvLeft , tvRight ;
112+ private Button selectVideoButton ;
112113 private Button encodeButton ;
114+ private Button cancelButton ;
113115 private MediaInfo videoInfo ;
114116 private File outputDestination ;
115117
@@ -118,17 +120,16 @@ protected void onCreate(Bundle savedInstanceState)
118120 {
119121 super .onCreate (savedInstanceState );
120122 setContentView (R .layout .activity_main );
121- final Button selectVideo = findViewById (R .id .selectVideo );
123+ selectVideoButton = findViewById (R .id .selectVideo );
122124 encodeButton = findViewById (R .id .encode );
125+ cancelButton = findViewById (R .id .cancel );
123126
124127 tvLeft = findViewById (R .id .tvLeft );
125128 tvRight = findViewById (R .id .tvRight );
126129
127130 videoView = findViewById (R .id .videoView );
128131 rangeSeekBar = findViewById (R .id .rangeSeekBar );
129- progressDialog = new ProgressDialog (this );
130- progressDialog .setTitle (null );
131- progressDialog .setCancelable (false );
132+ progressBar = findViewById (R .id .encodeProgress );
132133 rangeSeekBar .setEnabled (false );
133134
134135 containerSpinner = findViewById (R .id .containerSpinner );
@@ -153,7 +154,7 @@ public void onResult(Boolean result)
153154 }
154155 });
155156
156- selectVideo .setOnClickListener (new View .OnClickListener ()
157+ selectVideoButton .setOnClickListener (new View .OnClickListener ()
157158 {
158159 @ Override
159160 public void onClick (View v )
@@ -177,6 +178,15 @@ public void onClick(View v)
177178 startEncode ();
178179 }
179180 });
181+
182+ cancelButton .setOnClickListener (new View .OnClickListener ()
183+ {
184+ @ Override
185+ public void onClick (View v )
186+ {
187+ cancelEncode ();
188+ }
189+ });
180190 }
181191
182192 private void getPermission ()
@@ -379,10 +389,28 @@ private void startEncode()
379389 // Output file
380390 command .add (destination .getAbsolutePath ());
381391
382- FFmpegResponseHandler handler = new FFmpegResponseHandler (videoInfo .durationMs , progressDialog , _transcodeResultHandler );
392+ FFmpegResponseHandler handler = new FFmpegResponseHandler (videoInfo .durationMs , progressBar , _transcodeResultHandler );
383393 FFmpegUtil .call (command .toArray (new String [command .size ()]), handler );
384394
385395 stopVideoPlayback ();
396+
397+ selectVideoButton .setVisibility (View .GONE );
398+ encodeButton .setVisibility (View .GONE );
399+ cancelButton .setVisibility (View .VISIBLE );
400+ progressBar .setVisibility (View .VISIBLE );
401+ }
402+
403+ private void cancelEncode ()
404+ {
405+ FFmpegUtil .cancelCall ();
406+
407+ _transcodeResultHandler .onResult (false );
408+
409+ boolean result = outputDestination .delete ();
410+ if (result == false )
411+ {
412+ Log .d (TAG , "Failed to remove after encode cancel: " + outputDestination .getAbsolutePath ());
413+ }
386414 }
387415
388416 private void stopVideoPlayback ()
@@ -715,6 +743,11 @@ public void onClick(DialogInterface dialog, int which)
715743 .show ();
716744
717745 startVideoPlayback ();
746+
747+ selectVideoButton .setVisibility (View .VISIBLE );
748+ encodeButton .setVisibility (View .VISIBLE );
749+ cancelButton .setVisibility (View .GONE );
750+ progressBar .setVisibility (View .GONE );
718751 }
719752 };
720753
0 commit comments