package com.tejia.lijin.app.ui.trends;
|
|
import android.Manifest;
|
import android.content.Intent;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.Drawable;
|
import android.media.MediaPlayer;
|
import android.net.Uri;
|
import android.os.Bundle;
|
import android.os.Environment;
|
import android.os.Handler;
|
import android.os.Message;
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
import android.view.View;
|
import android.view.animation.Animation;
|
import android.view.animation.TranslateAnimation;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.SeekBar;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
import android.widget.VideoView;
|
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.util.security.MD5Utils;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.callBack.GeneralCallback;
|
import com.tejia.lijin.app.callBack.PermissionInterface;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.util.PermissionHelper;
|
import com.tejia.lijin.app.util.ToolUtil;
|
import com.ysh.wpc.appupdate.download.DownLoadFile;
|
|
import java.io.File;
|
|
import static com.tejia.lijin.app.ui.dialog.AddZFBInfoDialog2.Builder.TEXT_ALIGIN_MIDDLE;
|
|
/**
|
* Created by weikou2015 on 2018/8/22.
|
*/
|
|
public class VideoPlayActivity extends BaseActivity implements PermissionInterface, View.OnClickListener {
|
|
private VideoView vv_video;
|
//控制区域
|
private FrameLayout fl_controller, fl_top;
|
private LinearLayout fl_bottom;
|
private TextView tv_save;
|
private ImageView iv_back, iv_status;
|
private TextView tv_play_time, tv_total_time;
|
private SeekBar sb_progress;
|
|
private int errorCount = 0;
|
|
@Override
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setShowRecommend(false);
|
setContentView(R.layout.activity_play_video);
|
initView();
|
initData();
|
}
|
|
private void initView() {
|
vv_video = findViewById(R.id.vv_video);
|
fl_controller = findViewById(R.id.fl_controller);
|
tv_save = findViewById(R.id.tv_save);
|
iv_back = findViewById(R.id.iv_back);
|
iv_status = findViewById(R.id.iv_status);
|
tv_play_time = findViewById(R.id.tv_play_time);
|
tv_total_time = findViewById(R.id.tv_total_time);
|
sb_progress = findViewById(R.id.sb_progress);
|
|
fl_top = findViewById(R.id.fl_top);
|
fl_bottom = findViewById(R.id.fl_bottom);
|
|
tv_save.setOnClickListener(this);
|
iv_back.setOnClickListener(this);
|
vv_video.setOnClickListener(this);
|
iv_status.setOnClickListener(this);
|
|
Drawable drawable = getNewDrawable(R.drawable.icon_play_video_thumb, DimenUtils.dip2px(getApplicationContext(), 36), DimenUtils.dip2px(getApplicationContext(), 36));
|
sb_progress.setThumb(drawable);
|
|
sb_progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
@Override
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
if (fromUser) {
|
vv_video.seekTo(progress);
|
}
|
}
|
|
@Override
|
public void onStartTrackingTouch(SeekBar seekBar) {
|
|
}
|
|
@Override
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
|
}
|
});
|
}
|
|
|
//调用函数缩小图片
|
public BitmapDrawable getNewDrawable(int restId, int dstWidth, int dstHeight) {
|
Bitmap Bmp = BitmapFactory.decodeResource(
|
getResources(), restId);
|
Bitmap bmp = Bmp.createScaledBitmap(Bmp, dstWidth, dstHeight, true);
|
BitmapDrawable d = new BitmapDrawable(bmp);
|
Bitmap bitmap = d.getBitmap();
|
if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
|
d.setTargetDensity(getResources().getDisplayMetrics());
|
}
|
return d;
|
}
|
|
int duration = -1;
|
String videoUrl;//视频连接
|
|
private void initVideoView() {
|
videoUrl = getIntent().getStringExtra("url");
|
vv_video.setVideoPath(videoUrl);
|
vv_video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
@Override
|
public void onPrepared(MediaPlayer mp) {
|
duration = mp.getDuration();
|
initSeekBarData();
|
handler.sendEmptyMessage(1);
|
handler.sendEmptyMessageDelayed(2, 2000);
|
iv_status.setImageResource(R.drawable.icon_play_video_status_pause);
|
}
|
});
|
vv_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
@Override
|
public void onCompletion(MediaPlayer mp) {
|
if (errorCount > 2)
|
return;
|
vv_video.setVideoPath(videoUrl);
|
vv_video.start();
|
}
|
});
|
vv_video.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
@Override
|
public boolean onError(MediaPlayer mp, int what, int extra) {
|
|
ToolUtil.setGeneralPop(VideoPlayActivity.this,"温馨提示","糟糕,视频播放出错了。","确定",TEXT_ALIGIN_MIDDLE,new GeneralCallback(){
|
@Override
|
public void onSuccess() {
|
|
}
|
|
@Override
|
public void onError() {
|
|
}
|
});
|
return true;
|
}
|
});
|
|
|
vv_video.setOnInfoListener(new MediaPlayer.OnInfoListener() {
|
@Override
|
public boolean onInfo(MediaPlayer mp, int what, int extra) {
|
return false;
|
}
|
});
|
vv_video.setOnClickListener(this);
|
vv_video.start();
|
}
|
|
private String convertTime(int ms) {
|
if (ms < 0)
|
return "00:00";
|
int s = ms / 1000;
|
int hour = s / 3600;
|
int minute = (s % 3600) / 60;
|
int second = s % 60;
|
|
String st = "";
|
if (hour > 0) {
|
if (hour < 10)
|
st += "0";
|
st += hour + ":";
|
|
}
|
|
if (minute < 10)
|
st += "0";
|
st += minute + ":";
|
|
if (second < 10)
|
st += "0";
|
st += second;
|
|
return st;
|
}
|
|
private void initSeekBarData() {
|
tv_total_time.setText(convertTime(duration));
|
sb_progress.setMax(duration);
|
|
}
|
|
private void setPlayProgress(int playTime) {
|
tv_play_time.setText(convertTime(playTime));
|
}
|
|
|
private Handler handler = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
switch (msg.what) {
|
case 1:
|
if (errorCount > 2)
|
return;
|
//1.得到当前的视频播放进程
|
int currentPosition = vv_video.getCurrentPosition();//0
|
|
//2.SeekBar.setProgress(当前进度);
|
sb_progress.setProgress(currentPosition);
|
|
setPlayProgress(currentPosition);
|
|
//4.每秒更新一次
|
handler.removeMessages(1);
|
handler.sendEmptyMessageDelayed(1, 1000);
|
break;
|
|
case 2:
|
hiddenController();
|
break;
|
case 3:
|
Toast.makeText(VideoPlayActivity.this, "保存视频成功", Toast.LENGTH_SHORT).show();
|
break;
|
case 4:
|
Toast.makeText(VideoPlayActivity.this, "保存视频失败", Toast.LENGTH_SHORT).show();
|
break;
|
}
|
}
|
};
|
|
|
private void initData() {
|
mPermissionsChecker = new PermissionHelper(this, this);//权限
|
initVideoView();
|
}
|
|
|
int animationDuration = 500;
|
|
private synchronized void showController() {
|
if (fl_top.getAnimation() != null && fl_top.getAnimation().hasStarted())
|
return;
|
if (fl_top.getVisibility() == View.VISIBLE)
|
return;
|
|
iv_status.setVisibility(View.VISIBLE);
|
TranslateAnimation showAnimTop = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
|
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
|
showAnimTop.setDuration(animationDuration);
|
|
showAnimTop.setAnimationListener(new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
fl_top.setVisibility(View.VISIBLE);
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
handler.removeMessages(2);
|
handler.sendEmptyMessageDelayed(2, 3000);
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
}
|
});
|
|
|
TranslateAnimation showAnimBottom = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
|
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f);
|
showAnimBottom.setDuration(animationDuration);
|
|
showAnimBottom.setAnimationListener(new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
fl_bottom.setVisibility(View.VISIBLE);
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
}
|
});
|
|
fl_top.startAnimation(showAnimTop);
|
fl_bottom.startAnimation(showAnimBottom);
|
}
|
|
private synchronized void hiddenController() {
|
if (fl_top.getAnimation() != null && fl_top.getAnimation().hasStarted())
|
return;
|
if (fl_top.getVisibility() == View.GONE)
|
return;
|
if (!vv_video.isPlaying())
|
return;
|
iv_status.setVisibility(View.GONE);
|
TranslateAnimation hideAnimTop = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
|
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
|
hideAnimTop.setDuration(animationDuration);
|
|
hideAnimTop.setAnimationListener(new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
fl_top.setVisibility(View.GONE);
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
}
|
});
|
|
|
TranslateAnimation hideAnimBottom = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
|
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
|
hideAnimBottom.setDuration(animationDuration);
|
|
hideAnimBottom.setAnimationListener(new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
fl_bottom.setVisibility(View.GONE);
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
}
|
});
|
|
fl_top.startAnimation(hideAnimTop);
|
fl_bottom.startAnimation(hideAnimBottom);
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.vv_video:
|
if (fl_top.getVisibility() == View.VISIBLE) {
|
hiddenController();
|
} else {
|
showController();
|
}
|
break;
|
case R.id.iv_status:
|
if (vv_video.isPlaying()) {
|
vv_video.pause();
|
iv_status.setImageResource(R.drawable.icon_play_video_status_play);
|
showController();
|
} else {
|
vv_video.start();
|
iv_status.setImageResource(R.drawable.icon_play_video_status_pause);
|
//隐藏操作条
|
handler.removeMessages(2);
|
handler.sendEmptyMessageDelayed(2, 3000);
|
}
|
break;
|
case R.id.iv_back:
|
finish();
|
break;
|
case R.id.tv_save:
|
if (mPermissionsChecker.lacksPermissions(getPermissions())) { //检测权限
|
mPermissionsChecker.showNormalDialog();
|
} else {
|
saveVideo(videoUrl);
|
}
|
break;
|
}
|
}
|
|
|
/**
|
* 保存视频
|
*
|
* @param videoUrl
|
*/
|
public void saveVideo(final String videoUrl) {
|
final String fileName = MD5Utils.getMD532(videoUrl);
|
final String path = Environment.getExternalStorageDirectory().getPath() + "/BanLiVideo/" + fileName + ".mp4";
|
final File file = new File(path);
|
if (!file.isDirectory() && file.exists()) {
|
Toast.makeText(this, "视频已保存", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
|
new Thread() {
|
@Override
|
public void run() {
|
DownLoadFile df = new DownLoadFile();
|
File video = new File(file.getParentFile().getAbsolutePath(), file.getName());
|
try {
|
df.downLoadFile(null, video.getPath(), videoUrl, VideoPlayActivity.this);
|
handler.sendEmptyMessage(3);//视频保存成功
|
//通知系统保存成功
|
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
Uri uri = Uri.fromFile(video);
|
intent.setData(uri);
|
sendBroadcast(intent);
|
} catch (Exception e) {//视频保存失败
|
e.printStackTrace();
|
if (video.exists())
|
video.delete();
|
handler.sendEmptyMessage(4);
|
}
|
}
|
}.start();
|
}
|
|
protected PermissionHelper mPermissionsChecker; // 权限检测器 检测是否有储存卡写入读取权限
|
|
/**
|
* 权限请求码
|
*
|
* @return
|
*/
|
@Override
|
public int getPermissionsRequestCode() {
|
return 1001;
|
}
|
|
/**
|
* 请求权限
|
*
|
* @return
|
*/
|
@Override
|
public String[] getPermissions() {
|
return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE};
|
}
|
|
/**
|
* 权限请求成功
|
*/
|
@Override
|
public void requestPermissionsSuccess() {
|
tv_save.performClick();
|
}
|
|
/**
|
* 权限请求失败
|
*/
|
@Override
|
public void requestPermissionsFail() {
|
// Toast.makeText(this, "你以拒绝权限", Toast.LENGTH_SHORT).show();
|
}
|
|
/**
|
* 请求权限结果
|
*
|
* @param requestCode
|
* @param permissions
|
* @param grantResults
|
*/
|
@Override
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
if (mPermissionsChecker.requestPermissionsResult(requestCode, permissions, grantResults)) {
|
//权限请求结果,并已经处理了该回调
|
return;
|
}
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
}
|
|
int lastPosition = 0;
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
if (vv_video.isPlaying()) {
|
lastPosition = vv_video.getCurrentPosition();
|
iv_status.performClick();
|
}
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
if (lastPosition > 0) {
|
vv_video.seekTo(lastPosition);
|
sb_progress.setProgress(lastPosition);
|
if (!vv_video.isPlaying())
|
vv_video.start();
|
}
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
if (vv_video.isPlaying()) {
|
vv_video.pause();
|
}
|
}
|
}
|