diff --git a/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java index 87f3e79..331ad62 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java @@ -1,8 +1,11 @@ package com.frank.ffmpeg.activity; +import android.annotation.SuppressLint; import android.media.MediaPlayer; import android.os.Environment; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.text.TextUtils; import android.util.Log; import android.view.Surface; @@ -32,6 +35,22 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa private MediaPlayer mediaPlayer; private VideoPreviewBar videoPreviewBar; + private final static int TIME_UPDATE = 1000; + private final static int MSG_UPDATE = 1234; + + @SuppressLint("HandlerLeak") + private Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + if (msg.what == MSG_UPDATE) { + if (videoPreviewBar != null && mediaPlayer != null) { + videoPreviewBar.updateProgress(mediaPlayer.getCurrentPosition()); + } + mHandler.sendEmptyMessageDelayed(MSG_UPDATE, TIME_UPDATE); + } + } + }; @Override int getLayoutId() { @@ -80,6 +99,7 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa public void onPrepared(MediaPlayer mp) { Log.e(TAG, "onPrepared..."); mediaPlayer.start(); + mHandler.sendEmptyMessage(MSG_UPDATE); } }); } @@ -114,7 +134,7 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa public void onStopTracking(long progress) { if (mediaPlayer != null) { Log.e(TAG, "onStopTracking progress=" + progress); - mediaPlayer.seekTo((int) (progress / 1000)); + mediaPlayer.seekTo((int) progress); } } diff --git a/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java new file mode 100644 index 0000000..fddf183 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java @@ -0,0 +1,35 @@ +package com.frank.ffmpeg.util; + +import android.content.Context; +import android.util.DisplayMetrics; +import android.view.WindowManager; + +public class ScreenUtil { + + private static DisplayMetrics getDisplayMetrics(Context context) { + WindowManager windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); + if (windowManager == null) { + return null; + } + DisplayMetrics displayMetrics = new DisplayMetrics(); + windowManager.getDefaultDisplay().getMetrics(displayMetrics); + return displayMetrics; + } + + public static int getScreenWidth(Context context) { + if (context == null) { + return 0; + } + DisplayMetrics displayMetrics = getDisplayMetrics(context); + return displayMetrics != null ? displayMetrics.widthPixels : 0; + } + + public static int getScreenHeight(Context context) { + if (context == null) { + return 0; + } + DisplayMetrics displayMetrics = getDisplayMetrics(context); + return displayMetrics != null ? displayMetrics.heightPixels : 0; + } + +} diff --git a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java index 7919be9..bdcdc4a 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java +++ b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java @@ -11,9 +11,11 @@ import android.view.TextureView; import android.view.View; import android.widget.RelativeLayout; import android.widget.SeekBar; +import android.widget.TextView; import com.frank.ffmpeg.R; import com.frank.ffmpeg.hardware.HardwareDecode; +import com.frank.ffmpeg.util.TimeUtil; /** * 视频拖动实时预览的控件 @@ -30,10 +32,14 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On private HardwareDecode hardwareDecode; - private long duration; + private int duration; private PreviewBarCallback mPreviewBarCallback; + private TextView txtVideoProgress; + + private TextView txtVideoDuration; + public VideoPreviewBar(Context context) { super(context); initView(context); @@ -48,6 +54,8 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On View view = LayoutInflater.from(context).inflate(R.layout.preview_video, this); previewBar = view.findViewById(R.id.preview_bar); texturePreView = view.findViewById(R.id.texture_preview); + txtVideoProgress = view.findViewById(R.id.txt_video_progress); + txtVideoDuration = view.findViewById(R.id.txt_video_duration); setListener(); } @@ -93,7 +101,8 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On } previewBar.setProgress(progress); if (hardwareDecode != null && progress < duration) { - hardwareDecode.seekTo(progress); + // us to ms + hardwareDecode.seekTo(progress * 1000); } } @@ -113,9 +122,17 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On @Override public void onData(long duration) { + //us to ms + final int durationMs = (int) (duration / 1000); Log.i(TAG, "duration=" + duration); - this.duration = duration; - previewBar.setMax((int) duration); + this.duration = durationMs; + post(new Runnable() { + @Override + public void run() { + previewBar.setMax(durationMs); + txtVideoDuration.setText(TimeUtil.getVideoTime(durationMs)); + } + }); } public void init(String videoPath, PreviewBarCallback previewBarCallback) { @@ -123,6 +140,13 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On setPreviewCallback(videoPath, texturePreView); } + public void updateProgress(int progress) { + if (progress >= 0 && progress <= duration) { + txtVideoProgress.setText(TimeUtil.getVideoTime(progress)); + previewBar.setProgress(progress); + } + } + public void release() { if (hardwareDecode != null) { hardwareDecode.release(); diff --git a/app/src/main/res/layout/preview_video.xml b/app/src/main/res/layout/preview_video.xml index d19e659..6d987b6 100644 --- a/app/src/main/res/layout/preview_video.xml +++ b/app/src/main/res/layout/preview_video.xml @@ -2,21 +2,41 @@ + + + + + android:layout_marginStart="10dp" + android:layout_marginEnd="10dp" + android:layout_above="@+id/txt_video_progress"/>