Skip to content

Commit 90ef55a

Browse files
committed
Removing polling for resetting video playback
Previously a Handler would check once a second if the video has exceed the range selector's duration. This is now changed to use a timer. When the video is paused the timer is deleted until the video playback is resumed. As an additional simplification, the playback location is no longer saved when the video is paused; instead playback always restarts from the beginning.
1 parent fc80fe4 commit 90ef55a

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

app/src/main/java/protect/videotranscoder/activity/MainActivity.java

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import android.os.Build;
1515
import android.os.Bundle;
1616
import android.os.Environment;
17-
import android.os.Handler;
1817
import android.provider.DocumentsContract;
1918
import android.provider.MediaStore;
2019
import android.support.v4.app.ActivityCompat;
@@ -39,7 +38,10 @@
3938
import java.util.Calendar;
4039
import java.util.Collections;
4140
import java.util.List;
41+
import java.util.Locale;
4242
import java.util.Map;
43+
import java.util.Timer;
44+
import java.util.TimerTask;
4345

4446
import protect.videotranscoder.FFmpegResponseHandler;
4547
import protect.videotranscoder.FFmpegUtil;
@@ -51,11 +53,10 @@ public class MainActivity extends AppCompatActivity
5153
private static final int REQUEST_TAKE_GALLERY_VIDEO = 100;
5254
private VideoView videoView;
5355
private CrystalRangeSeekbar rangeSeekBar;
54-
private Runnable r;
56+
private Timer videoTimer = null;
5557
private ProgressDialog progressDialog;
5658
private Uri selectedVideoUri;
5759
private static final String TAG = "VideoTranscoder";
58-
private int stopPosition;
5960
private TextView tvLeft, tvRight;
6061
private String filePath;
6162
private int durationMs;
@@ -223,20 +224,50 @@ private void selectVideo()
223224
startActivityForResult(Intent.createChooser(intent, message), REQUEST_TAKE_GALLERY_VIDEO);
224225
}
225226

227+
private void stopVideoPlayback()
228+
{
229+
videoView.pause();
230+
if(videoTimer != null)
231+
{
232+
videoTimer.cancel();
233+
videoTimer = null;
234+
}
235+
}
236+
237+
private void startVideoPlayback()
238+
{
239+
stopVideoPlayback();
240+
241+
int startTimeSec = rangeSeekBar.getSelectedMinValue().intValue();
242+
int stopTimeSec = rangeSeekBar.getSelectedMaxValue().intValue();
243+
int durationSec = stopTimeSec - startTimeSec;
244+
245+
videoView.seekTo(startTimeSec * 1000);
246+
videoView.start();
247+
248+
videoTimer = new Timer();
249+
videoTimer.schedule(new TimerTask()
250+
{
251+
@Override
252+
public void run()
253+
{
254+
startVideoPlayback();
255+
}
256+
}, durationSec * 1000);
257+
}
258+
226259
@Override
227260
protected void onPause()
228261
{
229262
super.onPause();
230-
stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
231-
videoView.pause();
263+
stopVideoPlayback();
232264
}
233265

234266
@Override
235267
protected void onResume()
236268
{
237269
super.onResume();
238-
videoView.seekTo(stopPosition);
239-
videoView.start();
270+
startVideoPlayback();
240271
}
241272

242273
@Override
@@ -287,25 +318,12 @@ public void onPrepared(MediaPlayer mp)
287318
@Override
288319
public void valueChanged(Number minValue, Number maxValue)
289320
{
290-
videoView.seekTo(minValue.intValue() * 1000);
291321
tvLeft.setText(getTime(minValue.intValue()));
292322
tvRight.setText(getTime(maxValue.intValue()));
293-
}
294-
});
295323

296-
final Handler handler = new Handler();
297-
handler.postDelayed(r = new Runnable()
298-
{
299-
@Override
300-
public void run()
301-
{
302-
303-
if (videoView.getCurrentPosition() >= rangeSeekBar.getSelectedMaxValue().intValue() * 1000)
304-
videoView.seekTo(rangeSeekBar.getSelectedMinValue().intValue() * 1000);
305-
handler.postDelayed(r, 1000);
324+
startVideoPlayback();
306325
}
307-
}, 1000);
308-
326+
});
309327
}
310328
});
311329
}
@@ -369,8 +387,7 @@ public void onClick(DialogInterface dialog, int which)
369387
})
370388
.show();
371389

372-
videoView.seekTo(stopPosition);
373-
videoView.start();
390+
startVideoPlayback();
374391
}
375392
};
376393

@@ -405,8 +422,7 @@ private void executeCutVideoCommand(int startMs, int endMs)
405422
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationMs, progressDialog, _transcodeResultHandler);
406423
FFmpegUtil.call(complexCommand, handler);
407424

408-
stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
409-
videoView.pause();
425+
stopVideoPlayback();
410426
}
411427

412428
/**
@@ -439,8 +455,7 @@ private void executeCompressCommand()
439455
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationMs, progressDialog, _transcodeResultHandler);
440456
FFmpegUtil.call(complexCommand, handler);
441457

442-
stopPosition = videoView.getCurrentPosition();
443-
videoView.pause();
458+
stopVideoPlayback();
444459
}
445460

446461
/**
@@ -472,8 +487,7 @@ private void extractAudioVideo()
472487
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationMs, progressDialog, _transcodeResultHandler);
473488
FFmpegUtil.call(complexCommand, handler);
474489

475-
stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
476-
videoView.pause();
490+
stopVideoPlayback();
477491
}
478492

479493
/**

0 commit comments

Comments
 (0)