增加播放时长、更新播放进度

增加播放时长、更新播放进度
pull/107/head
xufulong 5 years ago
parent 4ffee8a1c5
commit 9101472c0a
  1. 22
      app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java
  2. 35
      app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java
  3. 32
      app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java
  4. 30
      app/src/main/res/layout/preview_video.xml

@ -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);
}
}

@ -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;
}
}

@ -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();

@ -2,21 +2,41 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/txt_video_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginStart="20dp"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:id="@+id/txt_video_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="20dp"
android:textColor="@color/colorPrimaryDark"/>
<SeekBar
android:id="@+id/preview_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"/>
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_above="@+id/txt_video_progress"/>
<TextureView
android:id="@+id/texture_preview"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_above="@+id/preview_bar"
android:layout_marginBottom="20dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp" />

Loading…
Cancel
Save