Bladeren bron

播放视频时,定时隐藏操作按钮

liuzhenxing1118 2 jaren geleden
bovenliggende
commit
7446fcd863

+ 11 - 2
app/src/main/java/com/xplora/xpchat/activity/PagerActivity.java

@@ -42,7 +42,6 @@ import com.xplora.xpchat.manager.FilePathUtils;
 import com.xplora.xpchat.manager.MediaPlayerUtils;
 import com.xplora.xpchat.manager.MediaRecorderUtils;
 import com.xplora.xpchat.manager.ScreenOnUtils;
-import com.xplora.xpchat.model.ContactBean;
 import com.xplora.xpchat.model.EmojiBean;
 import com.xplora.xpchat.model.GridModel;
 import com.xplora.xpchat.model.MessageBean;
@@ -581,7 +580,7 @@ public class PagerActivity extends BaseActivity implements Listener.PlayListener
                         //先暂停正在播放的
                         mPlayManager.cancelPlay();
                     }
-                    mPlayManager.play(bean);
+                    mPlayManager.startPlay(bean, true);
                 }
                 break;
             case Constant.MESSAGE_TYPE_PHOTO:
@@ -640,6 +639,16 @@ public class PagerActivity extends BaseActivity implements Listener.PlayListener
         ScreenOnUtils.addScreenOn(getWindow());
     }
 
+    @Override
+    public void onPlayPause(MessageBean bean) {
+
+    }
+
+    @Override
+    public void onPlayPrepared(MessageBean bean) {
+
+    }
+
     @Override
     public void onPlayFinish(MessageBean bean) {
         Log.d(TAG, "onPlayFinish");

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

@@ -1,6 +1,7 @@
 package com.xplora.xpchat.fragment;
 
 import android.annotation.SuppressLint;
+import android.app.Activity;
 import android.app.DialogFragment;
 import android.content.Context;
 import android.net.Uri;
@@ -16,10 +17,15 @@ import android.widget.RelativeLayout;
 import android.widget.TextView;
 
 import com.xplora.xpchat.R;
+import com.xplora.xpchat.activity.PagerActivity;
 import com.xplora.xpchat.model.MessageBean;
 import com.xplora.xpchat.observer.Listener;
 import com.xplora.xpchat.utils.Constant;
 
+import java.sql.Time;
+import java.util.Timer;
+import java.util.TimerTask;
+
 public class BaseActionFragment extends DialogFragment implements View.OnClickListener, View.OnTouchListener {
     public String TAG = "losion / xpchat :" +  getClass().getSimpleName();
     public Context mContext = null;
@@ -31,6 +37,7 @@ public class BaseActionFragment extends DialogFragment implements View.OnClickLi
     public ImageButton mShareBtn = null;
     public ImageButton mSaveFileBtn = null;
     public ImageButton mCloseBtn = null;
+    public Timer mTimer = null;
 
     public static BaseActionFragment newInstance(Context context) {
         BaseActionFragment fragment = new BaseActionFragment();
@@ -87,8 +94,41 @@ public class BaseActionFragment extends DialogFragment implements View.OnClickLi
     @SuppressLint("ClickableViewAccessibility")
     @Override
     public boolean onTouch(View v, MotionEvent event) {
+        changeButtonVisibility();
+        startTimer();
+        return false;
+    }
+
+    public void changeButtonVisibility() {
         int status = mButtonLayout.getVisibility();
         mButtonLayout.setVisibility(status == View.GONE ? View.VISIBLE : View.GONE);
-        return false;
+    }
+
+    public void setButtonVisibility(int status) {
+        mButtonLayout.setVisibility(status);
+    }
+
+    public void startTimer() {
+        cancelTimer();
+        mTimer = new Timer();
+        mTimer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                timerCallback();
+            }
+        }, 3 * 1000);
+    }
+
+    public void cancelTimer() {
+        if (mTimer != null)
+            mTimer.cancel();
+        mTimer = null;
+    }
+
+    public void timerCallback() {
+        Activity activity = (Activity)mContext;
+        activity.runOnUiThread(()->{
+            setButtonVisibility(View.GONE);
+        });
     }
 }

+ 22 - 25
app/src/main/java/com/xplora/xpchat/fragment/VideoFragment.java

@@ -1,9 +1,6 @@
 package com.xplora.xpchat.fragment;
 
-import android.app.DialogFragment;
 import android.content.Context;
-import android.media.MediaPlayer;
-import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.util.Log;
@@ -12,18 +9,12 @@ import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 import android.view.View;
 import android.view.ViewGroup;
-import android.view.Window;
-import android.widget.ImageButton;
-import android.widget.ImageView;
-import android.widget.TextView;
 
 import com.xplora.xpchat.R;
 import com.xplora.xpchat.manager.MediaPlayerUtils;
 import com.xplora.xpchat.model.MessageBean;
 import com.xplora.xpchat.observer.Listener;
 
-import java.io.IOException;
-
 public class VideoFragment extends BaseActionFragment implements Listener.PlayListener {
     private MediaPlayerUtils mMediaPlayerUtils = null;
     private SurfaceHolder mSurfaceHolder = null;
@@ -50,7 +41,7 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
             public void run() {
                 try {
                     mMediaPlayerUtils = new MediaPlayerUtils(VideoFragment.this, mSurfaceHolder, false);
-                    mMediaPlayerUtils.play(bean);
+                    mMediaPlayerUtils.startPlay(bean, false);
                 } catch (IllegalArgumentException | IllegalStateException e) {
                     e.printStackTrace();
                 }
@@ -71,23 +62,13 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
         } else if (v == mPlayPauseBtn) {
             if (mMediaPlayerUtils.getIsPlaying()) {
                 mMediaPlayerUtils.mediaPause();
-                setPlayStatus(false);
             } else {
                 mMediaPlayerUtils.mediaStart();
-                setPlayStatus(true);
             }
         }
     }
 
-    MediaPlayer.OnCompletionListener onPlayCompletion = new MediaPlayer.OnCompletionListener() {
-        @Override
-        public void onCompletion(MediaPlayer mMediaPlayer) {
-            mMediaPlayer.seekTo(0);
-            setPlayStatus(false);
-        }
-    };
-
-    private void setPlayStatus(boolean isPlaying) {
+    private void changePlayStatusImage(boolean isPlaying) {
         int resId = isPlaying ? R.drawable.action_pause : R.drawable.action_play;
         mPlayPauseBtn.setBackgroundResource(resId);
     }
@@ -99,24 +80,40 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
         mMediaPlayerUtils.mediaRelease();
     }
 
+    @Override
+    public void onPlayPrepared(MessageBean bean) {
+        changePlayStatusImage(false);
+        setButtonVisibility(View.VISIBLE);
+    }
+
     @Override
     public void onPlayStart(MessageBean bean) {
-        setPlayStatus(true);
+        changePlayStatusImage(true);
+        setButtonVisibility(View.GONE);
+    }
+
+    @Override
+    public void onPlayPause(MessageBean bean) {
+        changePlayStatusImage(false);
+        setButtonVisibility(View.VISIBLE);
+        cancelTimer();
     }
 
     @Override
     public void onPlayFinish(MessageBean bean) {
         mMediaPlayerUtils.mediaSeekTo(0);
-        setPlayStatus(false);
+        changePlayStatusImage(false);
+        setButtonVisibility(View.VISIBLE);
+        cancelTimer();
     }
 
     @Override
     public void onPlayCancel(MessageBean bean) {
-        setPlayStatus(false);
+        changePlayStatusImage(false);
     }
 
     @Override
     public void onPlayException(String exMessage) {
-        setPlayStatus(false);
+        changePlayStatusImage(false);
     }
 }

+ 62 - 69
app/src/main/java/com/xplora/xpchat/manager/MediaPlayerUtils.java

@@ -5,24 +5,23 @@ import android.media.AudioAttributes;
 import android.media.AudioFocusRequest;
 import android.media.AudioManager;
 import android.media.MediaPlayer;
-import android.net.Uri;
 import android.view.SurfaceHolder;
 
+import androidx.annotation.NonNull;
+
 import com.xplora.xpchat.ChatApplication;
 import com.xplora.xpchat.model.MessageBean;
 import com.xplora.xpchat.observer.Listener;
 import com.xplora.xpchat.utils.Constant;
-import com.xplora.xpchat.view.ChatException;
 
 import java.io.IOException;
 
 public class MediaPlayerUtils {
     private MediaPlayer mMediaPlayer = null;
     private Listener.PlayListener mPlayListener = null;
-    private boolean mIsPlaying = false;
-    private MessageBean mPlayingBean = new MessageBean();
     private SurfaceHolder mSurfaceHolder = null;
     private boolean mIsAutoRelease = true;
+    private MessageBean mPlayingBean = new MessageBean();
     private AudioFocusRequest mAudioFocusRequest = null;
 
     public MediaPlayerUtils(Listener.PlayListener listener) {
@@ -35,32 +34,9 @@ public class MediaPlayerUtils {
         mIsAutoRelease = isAutoRelease;
     }
 
-    public boolean play(MessageBean bean) {
+    public void startPlay(MessageBean bean, boolean autoPlay) {
         setPlayingBean(bean);
-
-        //播放
-        try {
-            if (bean.type == Constant.MESSAGE_TYPE_VIDEO) {
-                startPlay(bean.videoPath);
-            } else {
-                startPlay(bean.filePath);
-            }
-
-            //告诉view,开始
-            if (mPlayListener != null)
-                mPlayListener.onPlayStart(bean);
-
-            return true;
-        }
-        catch (ChatException ex) {
-            if (mPlayListener != null)
-                mPlayListener.onPlayException(ex.getMessage());
-            return false;
-        }
-    }
-
-    private void startPlay(String filePath) throws ChatException {
-        stopPlay();
+        mediaStop();
 
         try {
             requestFocus();
@@ -68,49 +44,71 @@ public class MediaPlayerUtils {
             //配置
             mMediaPlayer = new MediaPlayer();
             mMediaPlayer.reset();
-            mMediaPlayer.setDataSource(filePath);
-            //mMediaPlayer.setDataSource(this, Uri.parse(mPlayingBean.videoPath));
+            mMediaPlayer.setDataSource(bean.type == Constant.MESSAGE_TYPE_VIDEO ? bean.videoPath : bean.filePath);
             mMediaPlayer.setScreenOnWhilePlaying(true);
-            if (mSurfaceHolder != null)
+            if (mSurfaceHolder != null) {
                 mMediaPlayer.setDisplay(mSurfaceHolder);
+            }
             mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                 @Override
                 public void onCompletion(MediaPlayer mp) {
-                    finishPlay();
+                    onMediaCompletion();
+                }
+            });
+            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
+                @Override
+                public void onPrepared(MediaPlayer mp) {
+                    onMediaPrepared(autoPlay);
                 }
             });
-
-            //播放
             mMediaPlayer.prepare();
-            mMediaPlayer.start();
+        } catch (IOException ex) {
+            if (mPlayListener != null) {
+                mPlayListener.onPlayException(ex.getMessage());
+            }
+        }
+    }
 
-            setIsPlaying(true);
-        }  catch (IOException ex) {
-            setIsPlaying(false);
-            throw new ChatException("Chat Exception:",ex);
+    private void onMediaPrepared(boolean autoPlay) {
+        mMediaPlayer.seekTo(0);
+        if (mPlayListener != null) {
+            mPlayListener.onPlayPrepared(mPlayingBean);
+        }
+
+        if (autoPlay) {
+            mMediaPlayer.start();
+            if (mPlayListener != null) {
+                mPlayListener.onPlayStart(mPlayingBean);
+            }
         }
     }
 
     //语音播放完成
-    private void finishPlay() {
-        stopPlay();
-
+    private void onMediaCompletion() {
+        mediaStop();
         //告诉view,结束
         if (mPlayListener != null)
             mPlayListener.onPlayFinish(mPlayingBean);
     }
 
-    //语音播放完成
-    public void cancelPlay() {
-        stopPlay();
+    public void mediaStart() {
+        if (mMediaPlayer != null)
+            mMediaPlayer.start();
+        //告诉view,开始
+        if (mPlayListener != null)
+            mPlayListener.onPlayStart(mPlayingBean);
+    }
 
-        //告诉view,取消//
+    public void mediaPause() {
+        if (mMediaPlayer != null)
+            mMediaPlayer.pause();
+        //告诉view,开始
         if (mPlayListener != null)
-            mPlayListener.onPlayCancel(mPlayingBean);
+            mPlayListener.onPlayPause(mPlayingBean);
     }
 
     //onDestroy 时会调用,防止闪退时,没有释放//
-    private void stopPlay() {
+    private void mediaStop() {
         if (mIsAutoRelease) {
             if (mMediaPlayer != null) {
                 mMediaPlayer.setOnCompletionListener(null);
@@ -120,23 +118,9 @@ public class MediaPlayerUtils {
             mMediaPlayer = null;
             abandonFocus();
         }
-        setIsPlaying(false);
-    }
-
-    public void mediaStart() {
-        setIsPlaying(true);
-        if (mMediaPlayer != null)
-            mMediaPlayer.start();
-    }
-
-    public void mediaPause() {
-        setIsPlaying(false);
-        if (mMediaPlayer != null)
-            mMediaPlayer.pause();
     }
 
     public void mediaRelease() {
-        setIsPlaying(false);
         if (mMediaPlayer != null) {
             mMediaPlayer.release();
             mMediaPlayer = null;
@@ -144,6 +128,14 @@ public class MediaPlayerUtils {
         abandonFocus();
     }
 
+    //语音播放完成
+    public void cancelPlay() {
+        mediaStop();
+        //告诉view,取消//
+        if (mPlayListener != null)
+            mPlayListener.onPlayCancel(mPlayingBean);
+    }
+
     public void mediaSeekTo(int value) {
         if (mMediaPlayer != null)
             mMediaPlayer.seekTo(value);
@@ -173,14 +165,15 @@ public class MediaPlayerUtils {
         }
     }
 
-    public MessageBean getPlayingBean() {
-        return mPlayingBean;
-    }
-
     public void setPlayingBean(MessageBean mPlayingBean) {
         this.mPlayingBean = mPlayingBean;
     }
 
-    public boolean getIsPlaying() { return mIsPlaying; }
-    public void setIsPlaying(boolean isPlaying) { mIsPlaying = isPlaying; }
+    public boolean getIsPlaying() {
+        if (mMediaPlayer != null) {
+            return mMediaPlayer.isPlaying();
+        } else {
+            return false;
+        }
+    }
 }

+ 2 - 0
app/src/main/java/com/xplora/xpchat/observer/Listener.java

@@ -7,7 +7,9 @@ import java.util.ArrayList;
 public class Listener {
 
     public interface PlayListener {
+        void onPlayPrepared(MessageBean bean);
         void onPlayStart(MessageBean bean);
+        void onPlayPause(MessageBean bean);
         void onPlayFinish(MessageBean bean);
         void onPlayCancel(MessageBean bean);
         void onPlayException(String exMessage);

+ 14 - 13
app/src/main/res/layout/chat_fragment_video.xml

@@ -14,23 +14,11 @@
         android:scaleType="centerCrop"
         />
 
-    <TextView
-        android:id="@+id/mNoFileText"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_centerInParent="true"
-        android:fontFamily="@font/baijamjuree"
-        android:textFontWeight="600"
-        android:textSize="12dp"
-        android:textColor="@color/xp_nodata"
-        android:text="@string/no_file"
-        android:visibility="gone"
-        />
-
     <RelativeLayout
         android:id="@+id/mButtonLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
+        android:visibility="gone"
         >
 
         <ImageButton
@@ -73,4 +61,17 @@
             />
     </RelativeLayout>
 
+    <TextView
+        android:id="@+id/mNoFileText"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerInParent="true"
+        android:fontFamily="@font/baijamjuree"
+        android:textFontWeight="600"
+        android:textSize="12dp"
+        android:textColor="@color/xp_nodata"
+        android:text="@string/no_file"
+        android:visibility="gone"
+        />
+
 </RelativeLayout>