视频预览画面跟随进度条移动

视频预览画面跟随进度条移动
pull/107/head
xufulong 5 years ago
parent 9101472c0a
commit 6ba661a574
  1. 4
      app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java
  2. 40
      app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java

@ -97,7 +97,7 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override @Override
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
Log.e(TAG, "onPrepared..."); Log.i(TAG, "onPrepared...");
mediaPlayer.start(); mediaPlayer.start();
mHandler.sendEmptyMessage(MSG_UPDATE); mHandler.sendEmptyMessage(MSG_UPDATE);
} }
@ -133,7 +133,7 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa
@Override @Override
public void onStopTracking(long progress) { public void onStopTracking(long progress) {
if (mediaPlayer != null) { if (mediaPlayer != null) {
Log.e(TAG, "onStopTracking progress=" + progress); Log.i(TAG, "onStopTracking progress=" + progress);
mediaPlayer.seekTo((int) progress); mediaPlayer.seekTo((int) progress);
} }
} }

@ -15,6 +15,7 @@ import android.widget.TextView;
import com.frank.ffmpeg.R; import com.frank.ffmpeg.R;
import com.frank.ffmpeg.hardware.HardwareDecode; import com.frank.ffmpeg.hardware.HardwareDecode;
import com.frank.ffmpeg.util.ScreenUtil;
import com.frank.ffmpeg.util.TimeUtil; import com.frank.ffmpeg.util.TimeUtil;
/** /**
@ -40,6 +41,14 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On
private TextView txtVideoDuration; private TextView txtVideoDuration;
private boolean isShowing;
private int screenWidth;
private int moveEndPos = 0;
private int previewHalfWidth;
public VideoPreviewBar(Context context) { public VideoPreviewBar(Context context) {
super(context); super(context);
initView(context); initView(context);
@ -57,6 +66,23 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On
txtVideoProgress = view.findViewById(R.id.txt_video_progress); txtVideoProgress = view.findViewById(R.id.txt_video_progress);
txtVideoDuration = view.findViewById(R.id.txt_video_duration); txtVideoDuration = view.findViewById(R.id.txt_video_duration);
setListener(); setListener();
screenWidth = ScreenUtil.getScreenWidth(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (moveEndPos == 0) {
int previewWidth = texturePreView.getWidth();
previewHalfWidth = previewWidth / 2;
int marginEnd = 0;
MarginLayoutParams layoutParams = (MarginLayoutParams) texturePreView.getLayoutParams();
if (layoutParams != null) {
marginEnd = layoutParams.getMarginEnd();
}
moveEndPos = screenWidth - previewWidth - marginEnd;
Log.i(TAG, "previewWidth=" + previewWidth);
}
} }
private void setPreviewCallback(final String filePath, TextureView texturePreView) { private void setPreviewCallback(final String filePath, TextureView texturePreView) {
@ -104,15 +130,26 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On
// us to ms // us to ms
hardwareDecode.seekTo(progress * 1000); hardwareDecode.seekTo(progress * 1000);
} }
int percent = progress * screenWidth / duration;
if (percent > previewHalfWidth && percent < moveEndPos && texturePreView != null) {
texturePreView.setTranslationX(percent - previewHalfWidth);
}
} }
@Override @Override
public void onStartTrackingTouch(SeekBar seekBar) { public void onStartTrackingTouch(SeekBar seekBar) {
if (!isShowing && texturePreView != null) {
isShowing = true;
texturePreView.setVisibility(VISIBLE);
}
} }
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {
if (isShowing && texturePreView != null) {
isShowing = false;
texturePreView.setVisibility(GONE);
}
if (mPreviewBarCallback != null) { if (mPreviewBarCallback != null) {
mPreviewBarCallback.onStopTracking(seekBar.getProgress()); mPreviewBarCallback.onStopTracking(seekBar.getProgress());
} }
@ -131,6 +168,7 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On
public void run() { public void run() {
previewBar.setMax(durationMs); previewBar.setMax(durationMs);
txtVideoDuration.setText(TimeUtil.getVideoTime(durationMs)); txtVideoDuration.setText(TimeUtil.getVideoTime(durationMs));
texturePreView.setVisibility(GONE);
} }
}); });
} }

Loading…
Cancel
Save