|
@@ -1,5 +1,6 @@
|
|
package com.xplora.xpchat.fragment;
|
|
package com.xplora.xpchat.fragment;
|
|
|
|
|
|
|
|
+import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Handler;
|
|
@@ -9,15 +10,23 @@ import android.view.SurfaceHolder;
|
|
import android.view.SurfaceView;
|
|
import android.view.SurfaceView;
|
|
import android.view.View;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.ViewGroup;
|
|
|
|
+import android.widget.SeekBar;
|
|
|
|
+
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
|
|
|
import com.xplora.xpchat.R;
|
|
import com.xplora.xpchat.R;
|
|
import com.xplora.xpchat.manager.MediaPlayerUtils;
|
|
import com.xplora.xpchat.manager.MediaPlayerUtils;
|
|
import com.xplora.xpchat.model.MessageBean;
|
|
import com.xplora.xpchat.model.MessageBean;
|
|
import com.xplora.xpchat.observer.Listener;
|
|
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 {
|
|
public class VideoFragment extends BaseActionFragment implements Listener.PlayListener {
|
|
private MediaPlayerUtils mMediaPlayerUtils = null;
|
|
private MediaPlayerUtils mMediaPlayerUtils = null;
|
|
private SurfaceHolder mSurfaceHolder = null;
|
|
private SurfaceHolder mSurfaceHolder = null;
|
|
|
|
+ private Timer mProgressTimer = null;
|
|
|
|
|
|
public static VideoFragment newInstance(Context context) {
|
|
public static VideoFragment newInstance(Context context) {
|
|
VideoFragment fragment = new VideoFragment();
|
|
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);
|
|
View rootView = inflater.inflate(R.layout.chat_fragment_video, container, false);
|
|
SurfaceView surfaceView = (SurfaceView) rootView.findViewById(R.id.surfaceView);
|
|
SurfaceView surfaceView = (SurfaceView) rootView.findViewById(R.id.surfaceView);
|
|
mSurfaceHolder = surfaceView.getHolder();
|
|
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;
|
|
return rootView;
|
|
}
|
|
}
|
|
|
|
|
|
private void initVideo() {
|
|
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
|
|
@Override
|
|
@@ -82,29 +98,50 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onPlayPrepared(MessageBean bean) {
|
|
public void onPlayPrepared(MessageBean bean) {
|
|
|
|
+ Log.d(TAG, "onPlayPrepared: ");
|
|
changePlayStatusImage(false);
|
|
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
|
|
@Override
|
|
public void onPlayStart(MessageBean bean) {
|
|
public void onPlayStart(MessageBean bean) {
|
|
|
|
+ Log.d(TAG, "onPlayStart: ");
|
|
changePlayStatusImage(true);
|
|
changePlayStatusImage(true);
|
|
setButtonVisibility(View.GONE);
|
|
setButtonVisibility(View.GONE);
|
|
|
|
+ startProgressTimer();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onPlayPause(MessageBean bean) {
|
|
public void onPlayPause(MessageBean bean) {
|
|
|
|
+ Log.d(TAG, "onPlayPause: ");
|
|
changePlayStatusImage(false);
|
|
changePlayStatusImage(false);
|
|
setButtonVisibility(View.VISIBLE);
|
|
setButtonVisibility(View.VISIBLE);
|
|
cancelTimer();
|
|
cancelTimer();
|
|
|
|
+ cancelProgressTimer();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onPlayFinish(MessageBean bean) {
|
|
public void onPlayFinish(MessageBean bean) {
|
|
- mMediaPlayerUtils.mediaSeekTo(0);
|
|
|
|
|
|
+ Log.d(TAG, "onPlayFinish: ");
|
|
|
|
+ setVideoProgress(0, true);
|
|
changePlayStatusImage(false);
|
|
changePlayStatusImage(false);
|
|
setButtonVisibility(View.VISIBLE);
|
|
setButtonVisibility(View.VISIBLE);
|
|
cancelTimer();
|
|
cancelTimer();
|
|
|
|
+ cancelProgressTimer();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -116,4 +153,56 @@ public class VideoFragment extends BaseActionFragment implements Listener.PlayLi
|
|
public void onPlayException(String exMessage) {
|
|
public void onPlayException(String exMessage) {
|
|
changePlayStatusImage(false);
|
|
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);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
}
|
|
}
|