2222import com .otaliastudios .transcoder .internal .Logger ;
2323import com .otaliastudios .transcoder .sink .DataSink ;
2424import com .otaliastudios .transcoder .sink .DefaultDataSink ;
25+ import com .otaliastudios .transcoder .source .DataSource ;
26+ import com .otaliastudios .transcoder .source .TrimDataSource ;
27+ import com .otaliastudios .transcoder .source .UriDataSource ;
2528import com .otaliastudios .transcoder .strategy .DefaultAudioStrategy ;
2629import com .otaliastudios .transcoder .strategy .DefaultVideoStrategy ;
2730import com .otaliastudios .transcoder .strategy .RemoveTrackStrategy ;
4144
4245
4346public class TranscoderActivity extends AppCompatActivity implements
44- TranscoderListener ,
45- RadioGroup .OnCheckedChangeListener {
47+ TranscoderListener {
4648
4749 private static final String TAG = "DemoApp" ;
4850 private static final Logger LOG = new Logger (TAG );
@@ -81,50 +83,27 @@ public class TranscoderActivity extends AppCompatActivity implements
8183 private long mTrimStartUs = 0 ;
8284 private long mTrimEndUs = 0 ;
8385
84- private TextWatcher mTrimStartTextWatcher = new TextWatcher () {
86+ private RadioGroup .OnCheckedChangeListener mRadioGroupListener
87+ = new RadioGroup .OnCheckedChangeListener () {
8588 @ Override
86- public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
87- }
88-
89- @ Override
90- public void onTextChanged (CharSequence s , int start , int before , int count ) {
91- }
92-
93- @ Override
94- public void afterTextChanged (Editable s ) {
95- if (s .length () > 0 ) {
96- try {
97- mTrimStartUs = Long .valueOf (s .toString ()) * 1000000 ;
98- } catch (NumberFormatException e ) {
99- mTrimStartUs = 0 ;
100- LOG .w ("Failed to read trimStart value." );
101- }
102- }
89+ public void onCheckedChanged (RadioGroup group , int checkedId ) {
90+ syncParameters ();
10391 }
10492 };
105- private TextWatcher mTrimEndTextWatcher = new TextWatcher () {
93+
94+ private TextWatcher mTextListener = new TextWatcher () {
10695 @ Override
107- public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
108- }
96+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) { }
10997
11098 @ Override
111- public void onTextChanged (CharSequence s , int start , int before , int count ) {
112- }
99+ public void onTextChanged (CharSequence s , int start , int before , int count ) { }
113100
114101 @ Override
115102 public void afterTextChanged (Editable s ) {
116- if (s .length () > 0 ) {
117- try {
118- mTrimEndUs = Long .valueOf (s .toString ()) * 1000000 ;
119- } catch (NumberFormatException e ) {
120- mTrimEndUs = 0 ;
121- LOG .w ("Failed to read trimEnd value." );
122- }
123- }
103+ syncParameters ();
124104 }
125105 };
126106
127-
128107 @ SuppressLint ("SetTextI18n" )
129108 @ Override
130109 protected void onCreate (Bundle savedInstanceState ) {
@@ -160,13 +139,13 @@ protected void onCreate(Bundle savedInstanceState) {
160139 mAudioSampleRateGroup = findViewById (R .id .sampleRate );
161140 mAudioReplaceGroup = findViewById (R .id .replace );
162141
163- mAudioChannelsGroup .setOnCheckedChangeListener (this );
164- mVideoFramesGroup .setOnCheckedChangeListener (this );
165- mVideoResolutionGroup .setOnCheckedChangeListener (this );
166- mVideoAspectGroup .setOnCheckedChangeListener (this );
167- mAudioSampleRateGroup .setOnCheckedChangeListener (this );
168- mTrimStartView .addTextChangedListener (mTrimStartTextWatcher );
169- mTrimEndView .addTextChangedListener (mTrimEndTextWatcher );
142+ mAudioChannelsGroup .setOnCheckedChangeListener (mRadioGroupListener );
143+ mVideoFramesGroup .setOnCheckedChangeListener (mRadioGroupListener );
144+ mVideoResolutionGroup .setOnCheckedChangeListener (mRadioGroupListener );
145+ mVideoAspectGroup .setOnCheckedChangeListener (mRadioGroupListener );
146+ mAudioSampleRateGroup .setOnCheckedChangeListener (mRadioGroupListener );
147+ mTrimStartView .addTextChangedListener (mTextListener );
148+ mTrimEndView .addTextChangedListener (mTextListener );
170149 syncParameters ();
171150
172151 mAudioReplaceGroup .setOnCheckedChangeListener ((group , checkedId ) -> {
@@ -178,13 +157,10 @@ protected void onCreate(Bundle savedInstanceState) {
178157 .setType ("audio/*" ), REQUEST_CODE_PICK_AUDIO );
179158 }
180159 }
181- onCheckedChanged (group , checkedId );
160+ mRadioGroupListener . onCheckedChanged (group , checkedId );
182161 });
183- }
184162
185- @ Override
186- public void onCheckedChanged (RadioGroup group , int checkedId ) {
187- syncParameters ();
163+
188164 }
189165
190166 private void syncParameters () {
@@ -240,6 +216,21 @@ private void syncParameters() {
240216 .addResizer (new FractionResizer (fraction ))
241217 .frameRate (frames )
242218 .build ();
219+
220+ try {
221+ mTrimStartUs = Long .valueOf (mTrimStartView .getText ().toString ()) * 1000000 ;
222+ } catch (NumberFormatException e ) {
223+ mTrimStartUs = 0 ;
224+ LOG .w ("Failed to read trimStart value." );
225+ }
226+ try {
227+ mTrimEndUs = Long .valueOf (mTrimEndView .getText ().toString ()) * 1000000 ;
228+ } catch (NumberFormatException e ) {
229+ mTrimEndUs = 0 ;
230+ LOG .w ("Failed to read trimEnd value." );
231+ }
232+ if (mTrimStartUs < 0 ) mTrimStartUs = 0 ;
233+ if (mTrimEndUs < 0 ) mTrimEndUs = 0 ;
243234 }
244235
245236 private void setIsTranscoding (boolean isTranscoding ) {
@@ -311,27 +302,19 @@ private void transcode() {
311302 DataSink sink = new DefaultDataSink (mTranscodeOutputFile .getAbsolutePath ());
312303 TranscoderOptions .Builder builder = Transcoder .into (sink );
313304 if (mAudioReplacementUri == null ) {
314- if (mTrimStartUs > 0 || mTrimEndUs > 0 ) {
315- if (mTranscodeInputUri1 != null ) builder .addDataSource (this , mTranscodeInputUri1 , mTrimStartUs , mTrimEndUs );
316- if (mTranscodeInputUri2 != null ) builder .addDataSource (this , mTranscodeInputUri2 , mTrimStartUs , mTrimEndUs );
317- if (mTranscodeInputUri3 != null ) builder .addDataSource (this , mTranscodeInputUri3 , mTrimStartUs , mTrimEndUs );
318- }
319- else {
320- if (mTranscodeInputUri1 != null ) builder .addDataSource (this , mTranscodeInputUri1 );
321- if (mTranscodeInputUri2 != null ) builder .addDataSource (this , mTranscodeInputUri2 );
322- if (mTranscodeInputUri3 != null ) builder .addDataSource (this , mTranscodeInputUri3 );
305+ if (mTranscodeInputUri1 != null ) {
306+ DataSource source = new UriDataSource (this , mTranscodeInputUri1 );
307+ builder .addDataSource (new TrimDataSource (source , mTrimStartUs , mTrimEndUs ));
323308 }
309+ if (mTranscodeInputUri2 != null ) builder .addDataSource (this , mTranscodeInputUri2 );
310+ if (mTranscodeInputUri3 != null ) builder .addDataSource (this , mTranscodeInputUri3 );
324311 } else {
325- if (mTrimStartUs > 0 || mTrimEndUs > 0 ) {
326- if (mTranscodeInputUri1 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri1 , mTrimStartUs , mTrimEndUs );
327- if (mTranscodeInputUri2 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri2 , mTrimStartUs , mTrimEndUs );
328- if (mTranscodeInputUri3 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri3 , mTrimStartUs , mTrimEndUs );
329- }
330- else {
331- if (mTranscodeInputUri1 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri1 );
332- if (mTranscodeInputUri2 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri2 );
333- if (mTranscodeInputUri3 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri3 );
312+ if (mTranscodeInputUri1 != null ) {
313+ DataSource source = new UriDataSource (this , mTranscodeInputUri1 );
314+ builder .addDataSource (TrackType .VIDEO , new TrimDataSource (source , mTrimStartUs , mTrimEndUs ));
334315 }
316+ if (mTranscodeInputUri2 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri2 );
317+ if (mTranscodeInputUri3 != null ) builder .addDataSource (TrackType .VIDEO , this , mTranscodeInputUri3 );
335318 builder .addDataSource (TrackType .AUDIO , this , mAudioReplacementUri );
336319 }
337320 mTranscodeFuture = builder .setListener (this )
0 commit comments