Browse Source

添加了测试播放器进度条拖动功能

lyn 5 years ago
parent
commit
a6431e7f09

+ 66 - 11
app/src/main/java/com/haochuan/hciptvbasic/test/TestPlayerActivity.java

@@ -32,10 +32,16 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
     private LinearLayout bottomContainer;
     private SeekBar videoProgressBar;
     private TextView showTimeView;
+    private LinearLayout seekContainer;
+    private TextView seekPercentView;
+    private SeekBar seekBar;
+    private TextView seekTimeView;
 
     //全局参数
     private String testUrl = "https://gzhc-sxrj.oss-cn-shenzhen.aliyuncs.com/gzhc-djbl/djbl01.mp4";
     private int duration = 0;
+    private int seekPercent = 1;
+    private long seekDelay = 2000L;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -47,6 +53,13 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
         bottomContainer = findViewById(R.id.layout_bottom);
         videoProgressBar = findViewById(R.id.progress_bar);
         showTimeView = findViewById(R.id.current_total);
+        seekContainer = findViewById(R.id.seek_container);
+        seekPercentView = findViewById(R.id.seek_percent);
+        seekBar = findViewById(R.id.seek_bar);
+        seekTimeView = findViewById(R.id.seek_time);
+
+        loadingBar.setMax(100);
+        loadingBar.setProgress(0);
 
         loadingBar.setMax(100);
         loadingBar.setProgress(0);
@@ -61,12 +74,12 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
         switch (keyCode){
             case KeyEvent.KEYCODE_DPAD_LEFT:
                 if(event.getAction() == KeyEvent.ACTION_DOWN){
-                    seekForward();
+                    seek(true);
                 }
                 return true;
             case KeyEvent.KEYCODE_DPAD_RIGHT:
                 if(event.getAction() == KeyEvent.ACTION_DOWN){
-                    seekBack();
+                    seek(false);
                 }
                 return true;
             case KeyEvent.KEYCODE_DPAD_DOWN:
@@ -85,7 +98,10 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
                 }
                 break;
             case KeyEvent.KEYCODE_BACK:
-                showExitDialog();
+                if(event.getAction() == KeyEvent.ACTION_DOWN){
+                    playOrPause();
+                    showExitDialog();
+                }
                 break;
             default:
                 break;
@@ -98,17 +114,37 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
 
     /*------------------------------具体功能实现------------------------------*/
     //向前拖动
-    private void seekForward(){
-        hcPlayer.seek(hcPlayer.getCurrentPlayPosition() + 1000);
-        showBottomContainer();
-    }
+    private void seek(boolean back){
+
+        //移除延迟执行任务
+        hcPlayer.getRootView().removeCallbacks(delaySeekRunnable);
+
+        //显示seekContainer
+        if(seekContainer.getVisibility() != View.VISIBLE){
+            //显示进度条,并且读取当前进度
+            int currentPosition = hcPlayer.getCurrentPlayPosition();
+            int percent = percent(currentPosition,duration);
+            seekBar.setProgress(percent);
+            seekPercentView.setText(percent+" %");
+            seekTimeView.setText(timeToString(currentPosition));
+            seekContainer.setVisibility(View.VISIBLE);
+        }
 
-    //向后拖动
-    private void seekBack(){
-        hcPlayer.seek(hcPlayer.getCurrentPlayPosition() - 1000);
-        showBottomContainer();
+        //获取当前进度,根据back是否为真来决定前后拖动进度条
+        int curProgress = seekBar.getProgress();
+        int seekProgress = back ? (curProgress - seekPercent):(curProgress + seekPercent);
+        seekBar.setProgress(seekProgress);
+        seekPercentView.setText(seekProgress + " %");
+        int seekTime = duration*seekProgress/100;
+        String seekTimeStr = timeToString(seekTime);
+        Logger.d(String.format("seekTime:%s,seekTimeStr:%s",seekTime,seekTimeStr));
+        seekTimeView.setText(seekTimeStr);
+
+        //延迟执行seek任务
+        hcPlayer.getRootView().postDelayed(delaySeekRunnable,seekDelay);
     }
 
+
     //暂停或者启动
     private void playOrPause(){
         if(hcPlayer.isPrePared()){
@@ -132,6 +168,7 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
         hideBottomContainerDelay();
     }
 
+
     //立即刷新进度和时间
     private void refreshProgressAndTimeImmed(){
         hcPlayer.getRootView().post(showProgressAndTimeRunnable);
@@ -247,6 +284,7 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
 
 
     /*------------------------Runnable--------------------------*/
+    //显示底部栏任务
     private Runnable showProgressAndTimeRunnable =new Runnable() {
         @Override
         public void run() {
@@ -266,6 +304,23 @@ public class TestPlayerActivity extends AppCompatActivity implements IVideoPlaye
         }
     };
 
+    //延迟执行seek拖动任务
+    private Runnable delaySeekRunnable =new Runnable() {
+        @Override
+        public void run() {
+            //获取当前seekBar进度,转化为实际时间数,然后执行seek
+            int seekProgress = seekBar.getProgress();
+            int seekTime = duration*seekProgress/100;
+            Logger.d(String.format("duration:%s,seekProgress:%s,seekTime:%s",
+                    duration,seekProgress,seekTime));
+            hcPlayer.seek(seekTime);
+
+            //隐藏seekContainer
+            seekContainer.setVisibility(View.GONE);
+
+        }
+    };
+
 
     /*--------------------IVideoPlayer 接口实现---------------------*/
 

+ 57 - 0
app/src/main/res/layout/activity_test_player.xml

@@ -74,4 +74,61 @@
 
     </LinearLayout>
 
+    <LinearLayout
+        android:id="@+id/seek_container"
+        android:layout_width="match_parent"
+        android:visibility="gone"
+        android:layout_height="80dp"
+        android:paddingLeft="50dp"
+        android:paddingRight="50dp"
+        android:layout_marginLeft="11.3dp"
+        android:layout_marginBottom="11.3dp"
+        android:layout_marginRight="11.3dp"
+        android:layout_alignParentBottom="true"
+        android:layout_alignParentStart="true"
+        android:background="#b3000000"
+        android:gravity="center_vertical"
+        android:orientation="horizontal">
+
+        <!--左边播放/暂停按钮-->
+        <TextView
+            android:id="@+id/seek_percent"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="11.3dp"
+            android:text="1%"
+            android:textColor="#eaebec"
+            />
+
+        <!--进度条-->
+        <SeekBar
+            android:id="@+id/seek_bar"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:focusable="true"
+            android:gravity="center"
+            android:max="100"
+            android:maxHeight="4.0dp"
+            android:minHeight="4.0dp"
+            android:layout_marginLeft="8dp"
+            android:paddingBottom="8.0dp"
+            android:paddingLeft="10.6dp"
+            android:paddingRight="10.6dp"
+            android:paddingTop="8.0dp"
+            />
+
+        <!--播放进度/播放总时长显示-->
+        <TextView
+            android:id="@+id/seek_time"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="11.3dp"
+            android:text="00:00"
+            android:textColor="#eaebec"
+            android:textSize="18.6dp"
+            />
+
+    </LinearLayout>
+
 </RelativeLayout>