2626import android .widget .AdapterView ;
2727import android .widget .ArrayAdapter ;
2828import android .widget .Button ;
29+ import android .widget .EditText ;
2930import android .widget .ProgressBar ;
3031import android .widget .Spinner ;
3132import android .widget .TextView ;
@@ -102,7 +103,7 @@ public class MainActivity extends AppCompatActivity
102103 private Spinner videoCodecSpinner ;
103104 private Spinner fpsSpinner ;
104105 private Spinner resolutionSpinner ;
105- private Spinner videoBitrateSpinner ;
106+ private EditText videoBitrateValue ;
106107 private Spinner audioCodecSpinner ;
107108 private Spinner audioBitrateSpinner ;
108109 private Spinner audioSampleRateSpinner ;
@@ -136,7 +137,7 @@ protected void onCreate(Bundle savedInstanceState)
136137 videoCodecSpinner = findViewById (R .id .videoCodecSpinner );
137138 fpsSpinner = findViewById (R .id .fpsSpinner );
138139 resolutionSpinner = findViewById (R .id .resolutionSpinner );
139- videoBitrateSpinner = findViewById (R .id .videoBitrateSpinner );
140+ videoBitrateValue = findViewById (R .id .videoBitrateValue );
140141 audioCodecSpinner = findViewById (R .id .audioCodecSpinner );
141142 audioBitrateSpinner = findViewById (R .id .audioBitrateSpinner );
142143 audioSampleRateSpinner = findViewById (R .id .audioSampleRateSpinner );
@@ -282,11 +283,22 @@ private void startEncode()
282283 VideoCodec videoCodec = (VideoCodec )videoCodecSpinner .getSelectedItem ();
283284 String fps = (String )fpsSpinner .getSelectedItem ();
284285 String resolution = (String )resolutionSpinner .getSelectedItem ();
285- String videoBitrate = (String )videoBitrateSpinner .getSelectedItem ();
286286 AudioCodec audioCodec = (AudioCodec ) audioCodecSpinner .getSelectedItem ();
287- String audioBitrate = (String ) audioBitrateSpinner .getSelectedItem ();
288- String audioSampleRate = (String ) audioSampleRateSpinner .getSelectedItem ();
287+ Integer audioBitrate = (Integer ) audioBitrateSpinner .getSelectedItem ();
288+ Integer audioSampleRate = (Integer ) audioSampleRateSpinner .getSelectedItem ();
289289 String audioChannel = (String ) audioChannelSpinner .getSelectedItem ();
290+ int videoBitrate ;
291+
292+ try
293+ {
294+ String videoBitrateStr = videoBitrateValue .getText ().toString ();
295+ videoBitrate = Integer .parseInt (videoBitrateStr );
296+ }
297+ catch (NumberFormatException e )
298+ {
299+ Toast .makeText (this , R .string .videoBitrateValueInvalid , Toast .LENGTH_LONG ).show ();
300+ return ;
301+ }
290302
291303 File outputDir ;
292304
@@ -377,7 +389,7 @@ private void startEncode()
377389
378390 // Sample rate
379391 command .add ("-ar" );
380- command .add (audioSampleRate );
392+ command .add (Integer . toString ( audioSampleRate ) );
381393
382394 // Channels
383395 command .add ("-ac" );
@@ -414,6 +426,11 @@ private void cancelEncode()
414426 }
415427 }
416428
429+ private boolean isEncoding ()
430+ {
431+ return cancelButton .getVisibility () == View .VISIBLE ;
432+ }
433+
417434 private void stopVideoPlayback ()
418435 {
419436 videoView .pause ();
@@ -457,15 +474,19 @@ protected void onPause()
457474 protected void onResume ()
458475 {
459476 super .onResume ();
460- startVideoPlayback ();
477+
478+ if (isEncoding () == false )
479+ {
480+ startVideoPlayback ();
481+ }
461482 }
462483
463- private void setSpinnerSelection (Spinner spinner , String value )
484+ private void setSpinnerSelection (Spinner spinner , Object value )
464485 {
465486 for (int index = 0 ; index < spinner .getCount (); index ++)
466487 {
467488 String item = spinner .getItemAtPosition (index ).toString ();
468- if (item .equals (value ))
489+ if (item .equals (value . toString () ))
469490 {
470491 spinner .setSelection (index );
471492 break ;
@@ -528,16 +549,20 @@ public void onNothingSelected(AdapterView<?> parentView)
528549 }
529550 });
530551
531- String [] fps = new String [] {"24" , "23.98" , "25" , "29.97" , "30" , "50" };
552+ LinkedList <String > fps = new LinkedList <>(Arrays .asList ("24" , "23.98" , "25" , "29.97" , "30" , "50" ));
553+ if (videoInfo .videoFramerate != null && fps .contains (videoInfo .videoFramerate ) == false )
554+ {
555+ fps .addFirst (videoInfo .videoFramerate );
556+ }
532557 fpsSpinner .setAdapter (new ArrayAdapter <>(this , R .layout .spinner_textview , fps ));
533558
534- String [] resolution = new String [] {"176x144" , "320x240" , "480x360" , "640x360" , "640x480" , "800x600" , "960x720" , "1024x768" , "1280x720" , "1920x1080" , "2048x1080" , "2048x858" , "2560x1440" , "2560x1600" , "4096x2160" };
559+ LinkedList <String > resolution = new LinkedList <>(Arrays .asList ("176x144" , "320x240" , "480x360" , "640x360" , "640x480" , "800x600" , "960x720" , "1024x768" , "1280x720" , "1920x1080" , "2048x1080" , "2048x858" , "2560x1440" , "2560x1600" , "4096x2160" ));
560+ if (videoInfo .videoResolution != null && resolution .contains (videoInfo .videoResolution ) == false )
561+ {
562+ resolution .addFirst (videoInfo .videoResolution );
563+ }
535564 resolutionSpinner .setAdapter (new ArrayAdapter <>(this , R .layout .spinner_textview , resolution ));
536565
537- // TODO: Should be a text field, not a spinner
538- String [] videoBitrate = new String [] {"500" };
539- videoBitrateSpinner .setAdapter (new ArrayAdapter <>(this , R .layout .spinner_textview , videoBitrate ));
540-
541566 audioCodecSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener ()
542567 {
543568 @ Override
@@ -559,10 +584,20 @@ public void onNothingSelected(AdapterView<?> parentView)
559584 }
560585 });
561586
562- String [] audioBitrate = new String [] {"15" , "24" , "32" , "64" , "96" , "128" , "192" , "256" , "320" , "384" , "448" , "512" };
587+ List <Integer > audioBitrate = new ArrayList <>(Arrays .asList (15 , 24 , 32 , 64 , 96 , 128 , 192 , 256 , 320 , 384 , 448 , 512 ));
588+ if (videoInfo .audioBitrate != null && audioBitrate .contains (videoInfo .audioBitrate ) == false )
589+ {
590+ audioBitrate .add (videoInfo .audioBitrate );
591+ Collections .sort (audioBitrate );
592+ }
563593 audioBitrateSpinner .setAdapter (new ArrayAdapter <>(this , R .layout .spinner_textview , audioBitrate ));
564594
565- String [] sampleRate = new String [] {"8000" , "11025" , "16000" , "22050" , "24000" , "32000" , "44100" , "48000" };
595+ List <Integer > sampleRate = new ArrayList <>(Arrays .asList (8000 , 11025 , 16000 , 22050 , 24000 , 32000 , 44100 , 48000 ));
596+ if (videoInfo .audioSampleRate != null && audioBitrate .contains (videoInfo .audioSampleRate ) == false )
597+ {
598+ sampleRate .add (videoInfo .audioSampleRate );
599+ Collections .sort (sampleRate );
600+ }
566601 audioSampleRateSpinner .setAdapter (new ArrayAdapter <>(this , R .layout .spinner_textview , sampleRate ));
567602
568603 String [] channels = new String [] {"1" , "2" };
@@ -590,19 +625,23 @@ public void onNothingSelected(AdapterView<?> parentView)
590625
591626 if (videoInfo .videoBitrate != null )
592627 {
593- setSpinnerSelection ( videoBitrateSpinner , videoInfo .videoBitrate );
628+ videoBitrateValue . setText ( Integer . toString ( videoInfo .videoBitrate ) );
594629 }
595630
596631 if (videoInfo .audioCodec != null )
597632 {
598633 setSpinnerSelection (audioCodecSpinner , videoInfo .audioCodec .toString ());
599634 }
600635
601- if (videoInfo .audioBitrate != null )
636+ Integer defaultAudioBitrate = videoInfo .audioBitrate ;
637+ if (defaultAudioBitrate == null )
602638 {
603- setSpinnerSelection (audioBitrateSpinner , videoInfo .audioBitrate );
639+ // Set some default if none is detected
640+ defaultAudioBitrate = 128 ;
604641 }
605642
643+ setSpinnerSelection (audioBitrateSpinner , defaultAudioBitrate );
644+
606645 if (videoInfo .audioSampleRate != null )
607646 {
608647 setSpinnerSelection (audioSampleRateSpinner , videoInfo .audioSampleRate );
@@ -656,7 +695,10 @@ public void valueChanged(Number minValue, Number maxValue)
656695 tvLeft .setText (getTime (minValue .intValue ()));
657696 tvRight .setText (getTime (maxValue .intValue ()));
658697
659- startVideoPlayback ();
698+ if (isEncoding () == false )
699+ {
700+ startVideoPlayback ();
701+ }
660702 }
661703 });
662704 }
@@ -673,8 +715,8 @@ public void onResult(MediaInfo result)
673715 {
674716 // Could not query the file, fill in what we know.
675717 result = new MediaInfo (videoFile , 0 , MediaContainer .MP4 , VideoCodec .MPEG4 , "640x480" ,
676- " 800" , "25" , AudioCodec .MP3 ,
677- " 44100" , " 128" , 2 );
718+ 800 , "25" , AudioCodec .MP3 ,
719+ 44100 , 128 , 2 );
678720 }
679721
680722 videoInfo = result ;
0 commit comments