liuzhenxing1118 2 лет назад
Родитель
Сommit
99154e492d

+ 1 - 0
app/src/main/java/com/xplora/xpchat/activity/ContactsActivity.java

@@ -24,6 +24,7 @@ import com.xplora.xpchat.observer.ContactContentObserver;
 import com.xplora.xpchat.utils.Constant;
 import com.xplora.xpchat.utils.Macros;
 import com.xplora.xpchat.utils.MetaData;
+import com.xplora.xpchat.utils.ToolsUtils;
 
 import java.util.ArrayList;
 import java.util.List;

+ 17 - 1
app/src/main/java/com/xplora/xpchat/fragment/BaseActionFragment.java

@@ -14,6 +14,7 @@ import android.view.Window;
 import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.RelativeLayout;
+import android.widget.SeekBar;
 import android.widget.TextView;
 
 import com.xplora.xpchat.R;
@@ -31,13 +32,17 @@ public class BaseActionFragment extends DialogFragment implements View.OnClickLi
     public Context mContext = null;
     public int position;
     public MessageBean bean;
+    public Timer mTimer = null;
 
     public RelativeLayout mButtonLayout = null;
     public ImageButton mPlayPauseBtn = null;
     public ImageButton mShareBtn = null;
     public ImageButton mSaveFileBtn = null;
     public ImageButton mCloseBtn = null;
-    public Timer mTimer = null;
+    public RelativeLayout mTimeLayout = null;
+    public TextView mTimeStartTv = null;
+    public TextView mTimeEndTv = null;
+    public SeekBar mTimeSeekbar = null;
 
     public static BaseActionFragment newInstance(Context context) {
         BaseActionFragment fragment = new BaseActionFragment();
@@ -62,6 +67,11 @@ public class BaseActionFragment extends DialogFragment implements View.OnClickLi
         mSaveFileBtn = (ImageButton)rootView.findViewById(R.id.mSaveFileBtn);
         mCloseBtn = (ImageButton)rootView.findViewById(R.id.mCloseBtn);
 
+        mTimeLayout = (RelativeLayout)rootView.findViewById(R.id.mTimeLayout);
+        mTimeStartTv = (TextView)rootView.findViewById(R.id.mTimeStartText);
+        mTimeEndTv = (TextView)rootView.findViewById(R.id.mTimeEndText);
+        mTimeSeekbar = (SeekBar) rootView.findViewById(R.id.seekbar);
+
         if (mPlayPauseBtn != null) {
             mPlayPauseBtn.setOnClickListener(this);
         }
@@ -102,10 +112,16 @@ public class BaseActionFragment extends DialogFragment implements View.OnClickLi
     public void changeButtonVisibility() {
         int status = mButtonLayout.getVisibility();
         mButtonLayout.setVisibility(status == View.GONE ? View.VISIBLE : View.GONE);
+        if (mTimeLayout != null) {
+            mTimeLayout.setVisibility(mButtonLayout.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
+        }
     }
 
     public void setButtonVisibility(int status) {
         mButtonLayout.setVisibility(status);
+        if (mTimeLayout != null) {
+            mTimeLayout.setVisibility(mButtonLayout.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
+        }
     }
 
     public void startTimer() {

+ 103 - 14
app/src/main/java/com/xplora/xpchat/fragment/VideoFragment.java

@@ -1,5 +1,6 @@
 package com.xplora.xpchat.fragment;
 
+import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.Handler;
@@ -9,15 +10,23 @@ import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.SeekBar;
+
+import androidx.annotation.NonNull;
 
 import com.xplora.xpchat.R;
 import com.xplora.xpchat.manager.MediaPlayerUtils;
 import com.xplora.xpchat.model.MessageBean;
 import com.xplora.xpchat.observer.Listener;
+import com.xplora.xpchat.utils.ToolsUtils;
+
+import java.util.Timer;
+import java.util.TimerTask;
 
 public class VideoFragment extends BaseActionFragment implements Listener.PlayListener {
     private MediaPlayerUtils mMediaPlayerUtils = null;
     private SurfaceHolder mSurfaceHolder = null;
+    private Timer mProgressTimer = null;
 
     public static VideoFragment newInstance(Context context) {
         VideoFragment fragment = new VideoFragment();
@@ -30,23 +39,30 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
         View rootView = inflater.inflate(R.layout.chat_fragment_video, container, false);
         SurfaceView surfaceView = (SurfaceView) rootView.findViewById(R.id.surfaceView);
         mSurfaceHolder = surfaceView.getHolder();
-        onCreateViewEx(rootView);
+        mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
+            @Override
+            public void surfaceCreated(@NonNull SurfaceHolder holder) {
+                initVideo();
+            }
+
+            @Override
+            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
 
-        initVideo();
+            }
+
+            @Override
+            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
+
+            }
+        });
+        onCreateViewEx(rootView);
+        mTimeSeekbar.setOnSeekBarChangeListener(onSeekBarChangeListener);
         return rootView;
     }
 
     private void initVideo() {
-        new Handler().postDelayed(new Runnable() {
-            public void run() {
-                try {
-                    mMediaPlayerUtils = new MediaPlayerUtils(VideoFragment.this, mSurfaceHolder, false);
-                    mMediaPlayerUtils.startPlay(bean, false);
-                } catch (IllegalArgumentException | IllegalStateException e) {
-                    e.printStackTrace();
-                }
-            }
-        }, 30);
+        mMediaPlayerUtils = new MediaPlayerUtils(VideoFragment.this, mSurfaceHolder, false);
+        mMediaPlayerUtils.startPlay(bean, false);
     }
 
     @Override
@@ -82,29 +98,50 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
 
     @Override
     public void onPlayPrepared(MessageBean bean) {
+        Log.d(TAG, "onPlayPrepared: ");
         changePlayStatusImage(false);
-        setButtonVisibility(View.VISIBLE);
+        new Timer().schedule(new TimerTask() {
+            @Override
+            public void run() {
+                Activity activity = (Activity)mContext;
+                activity.runOnUiThread(()->{
+                    setButtonVisibility(View.VISIBLE);
+                });
+            }
+        }, 500);
+
+        int total = mMediaPlayerUtils.getTotalSeconds();
+        mTimeStartTv.setText(ToolsUtils.seconds2Time(0));
+        mTimeEndTv.setText(ToolsUtils.seconds2Time(total));
+        mTimeSeekbar.setMin(0);
+        mTimeSeekbar.setMax(total);
     }
 
     @Override
     public void onPlayStart(MessageBean bean) {
+        Log.d(TAG, "onPlayStart: ");
         changePlayStatusImage(true);
         setButtonVisibility(View.GONE);
+        startProgressTimer();
     }
 
     @Override
     public void onPlayPause(MessageBean bean) {
+        Log.d(TAG, "onPlayPause: ");
         changePlayStatusImage(false);
         setButtonVisibility(View.VISIBLE);
         cancelTimer();
+        cancelProgressTimer();
     }
 
     @Override
     public void onPlayFinish(MessageBean bean) {
-        mMediaPlayerUtils.mediaSeekTo(0);
+        Log.d(TAG, "onPlayFinish: ");
+        setVideoProgress(0, true);
         changePlayStatusImage(false);
         setButtonVisibility(View.VISIBLE);
         cancelTimer();
+        cancelProgressTimer();
     }
 
     @Override
@@ -116,4 +153,56 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
     public void onPlayException(String exMessage) {
         changePlayStatusImage(false);
     }
+
+    public SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
+        @Override
+        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+
+        }
+
+        @Override
+        public void onStartTrackingTouch(SeekBar seekBar) {
+
+        }
+
+        @Override
+        public void onStopTrackingTouch(SeekBar seekBar) {
+            int progress = seekBar.getProgress();
+            setVideoProgress(progress, true);
+        }
+    };
+
+    public void setVideoProgress(int progress, boolean isSeekTo) {
+        if (isSeekTo) {
+            mMediaPlayerUtils.mediaSeekTo(progress);
+        }
+        mTimeSeekbar.setProgress(progress);
+        mTimeStartTv.setText(ToolsUtils.seconds2Time(progress));
+    }
+
+    public void startProgressTimer() {
+        cancelTimer();
+        mProgressTimer = new Timer();
+        mProgressTimer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                timerProgressCallback();
+            }
+        },  0, 1000);
+    }
+
+    public void cancelProgressTimer() {
+        if (mProgressTimer != null)
+            mProgressTimer.cancel();
+        mProgressTimer = null;
+    }
+
+    public void timerProgressCallback() {
+        Activity activity = (Activity)mContext;
+        activity.runOnUiThread(()->{
+            int progress = mMediaPlayerUtils.getCurrentSeconds();
+            Log.d(TAG, "timerProgressCallback: " + progress);
+            setVideoProgress(progress, false);
+        });
+    }
 }

+ 16 - 8
app/src/main/java/com/xplora/xpchat/manager/MediaPlayerUtils.java

@@ -17,12 +17,12 @@ import com.xplora.xpchat.utils.Constant;
 import java.io.IOException;
 
 public class MediaPlayerUtils {
-    private MediaPlayer mMediaPlayer = null;
-    private Listener.PlayListener mPlayListener = null;
-    private SurfaceHolder mSurfaceHolder = null;
-    private boolean mIsAutoRelease = true;
-    private MessageBean mPlayingBean = new MessageBean();
-    private AudioFocusRequest mAudioFocusRequest = null;
+    public MediaPlayer mMediaPlayer = null;
+    public Listener.PlayListener mPlayListener = null;
+    public SurfaceHolder mSurfaceHolder = null;
+    public boolean mIsAutoRelease = true;
+    public MessageBean mPlayingBean = new MessageBean();
+    public AudioFocusRequest mAudioFocusRequest = null;
 
     public MediaPlayerUtils(Listener.PlayListener listener) {
         mPlayListener = listener;
@@ -61,7 +61,7 @@ public class MediaPlayerUtils {
                     onMediaPrepared(autoPlay);
                 }
             });
-            mMediaPlayer.prepare();
+            mMediaPlayer.prepareAsync();
         } catch (IOException ex) {
             if (mPlayListener != null) {
                 mPlayListener.onPlayException(ex.getMessage());
@@ -138,7 +138,7 @@ public class MediaPlayerUtils {
 
     public void mediaSeekTo(int value) {
         if (mMediaPlayer != null)
-            mMediaPlayer.seekTo(value);
+            mMediaPlayer.seekTo(value * 1000);
     }
 
     private void requestFocus() {
@@ -176,4 +176,12 @@ public class MediaPlayerUtils {
             return false;
         }
     }
+
+    public int getTotalSeconds() {
+        return mMediaPlayer.getDuration() / 1000;
+    }
+
+    public int getCurrentSeconds() {
+        return mMediaPlayer.getCurrentPosition() / 1000;
+    }
 }

+ 29 - 0
app/src/main/java/com/xplora/xpchat/utils/ToolsUtils.java

@@ -40,6 +40,7 @@ import java.util.Random;
 import com.xplora.xpchat.R;
 
 public class ToolsUtils {
+    public static String TAG = "losion / xpchat :" +  "ToolsUtils";
     public static boolean isNullOrEmpty(String string) {
         if (string == null || string.isEmpty())
             return true;
@@ -162,6 +163,34 @@ public class ToolsUtils {
         }
     }
 
+    public static String seconds2Time(int value) {
+        Log.d(TAG, "seconds2Time: " + value);
+        value = Math.round(value);
+        String time = "";
+        int hour = 0;
+        int min = 0;
+        int second = 0;
+        if (value < 60) {
+            second = value;
+        } else if (value < 60*60) {
+            min = value / 60;
+            second = value % 60;
+        } else {
+            hour = value / 60 / 60;
+            min = value / 60 - (hour * 60);
+            second = value % 60;
+        }
+
+        if (hour > 0) {
+            time += hour >= 10 ? hour : "0" + hour;
+            time += ":";
+        }
+        time += min >= 10 ? min : "0" + min;
+        time += ":";
+        time += second >= 10 ? second : "0" + second;
+        return time;
+    }
+
     public static void setTimeMark(List<MessageBean> arrayList, long lastShowTime) {
         for (int i = 0; i < arrayList.size(); i++) {
             MessageBean back = arrayList.get(i);

+ 48 - 0
app/src/main/res/layout/chat_fragment_video.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -61,6 +62,53 @@
             />
     </RelativeLayout>
 
+    <RelativeLayout
+        android:id="@+id/mTimeLayout"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_alignParentBottom="true"
+        android:visibility="gone"
+        >
+
+        <TextView
+            android:id="@+id/mTimeStartText"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="5dp"
+            android:layout_marginStart="5dp"
+            android:fontFamily="@font/baijamjuree"
+            android:textSize="9sp"
+            android:textColor="@color/white"
+            android:text="00:00"
+            tools:ignore="SmallSp" />
+
+        <TextView
+            android:id="@+id/mTimeEndText"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="5dp"
+            android:layout_marginEnd="5dp"
+            android:layout_alignParentEnd="true"
+            android:fontFamily="@font/baijamjuree"
+            android:textSize="9sp"
+            android:textColor="@color/white"
+            android:text="00:30"
+            tools:ignore="SmallSp" />
+
+        <SeekBar
+            android:id="@+id/seekbar"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_centerHorizontal="true"
+            android:layout_marginTop="20dp"
+            android:layout_marginBottom="6dp"
+            style="@style/Widget.AppCompat.SeekBar"
+            android:progressTint="@color/xp_green"
+            />
+
+    </RelativeLayout>
+
+
     <TextView
         android:id="@+id/mNoFileText"
         android:layout_width="wrap_content"