可以直接复制下面代码直接使用
实现进度条,按下home键从后台回来后处理,自定义控件
1.项目文件结构
2.MainActivity内代码
package io.com.layouttest;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private boolean isPlay=true;
private SeekBar seekBar;
private VideoView videoView;
private TextView s1,s2;
ImageView play1,setMax;
LinearLayout isShow;
int start1=0;
int total=0;
int width=0;
int height=0;
int ClickNum=0;//横竖屏切换
float scale;
RelativeLayout Layout;
RelativeLayout.LayoutParams layoutParams;
//开启一个递归定时器
Handler handler1=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if(msg.what==1){
handler1.removeMessages(1);
progress();//执行更新
if(!isPlay){
handler1.sendEmptyMessageDelayed(1, 1000);
}
}
return false;
}
});
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialization();
//监听videoview是否准备就绪
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("这里是","准备就绪");
if(start1==0){
progress();
}
//监听videoViewseekTo是否设置完成
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("这里是","已经设置完成了");
}
});
}
});
//监听videoView是否播放完成
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stop1();
}
});
//点击videoView,操作控件显示关闭
videoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(isShow.getVisibility()==View.VISIBLE){
isShow.setVisibility(View.INVISIBLE);
}else {
isShow.setVisibility(View.VISIBLE);
}
return false;
}
});
//监听seekBar
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//进度已更改
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// int progress1= (int) Math.floor(progress / 100f * total);
if(fromUser){
progress();
videoView.seekTo(progress);
}
}
//刚开始拖动
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
//拖动完成
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//
//在视频没有初始化之前禁止拖动
seekBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(total==0){
return true;
}else {
return false;
}
}
});
//播放按钮被点击
play1.setOnClickListener(this);
//放大按钮被点击
setMax.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.play1){
//播放按钮被点击
if(isPlay){
//可以通过
play1();
}else {
stop1();
}
}else if(v.getId()==R.id.setMax){
//旋转屏幕
if(ClickNum==0){
//隐藏状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
layoutParams.width= height;
layoutParams.height= width;
Layout.setLayoutParams(layoutParams);
ClickNum=1;
}else if (ClickNum==1){
//竖屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
layoutParams.width=width;
//这里的500是px,设置时根据像素密度设置
layoutParams.height=(int) (200 * scale + 0.5f);
Layout.setLayoutParams(layoutParams);
ClickNum=0;
}
}
}
//播放
private void play1(){
videoView.start();
play1.setImageResource(R.drawable.stop);
isPlay=false;
handler1.sendEmptyMessage(1);
}
//暂停
private void stop1(){
videoView.pause();
play1.setImageResource(R.drawable.play);
isPlay=true;
}
//初始化数据
private void initialization(){
videoView = (VideoView) findViewById(R.id.video1);
// videoView.setVideoPath("");//可以填写网络地址
String uri = "android.resource://" + getPackageName() + "/" + R.raw.wanjia;
videoView.setVideoURI(Uri.parse(uri));
//初始数据
play1=(ImageView) findViewById(R.id.play1);
seekBar=(SeekBar) findViewById(R.id.SeekBar);
s1=(TextView) findViewById(R.id.s1);
s2=(TextView) findViewById(R.id.s2);
setMax=(ImageView) findViewById(R.id.setMax);
isShow=(LinearLayout) findViewById(R.id.isShow);
//旋转屏幕使用
Layout=(RelativeLayout) findViewById(R.id.videoP);
layoutParams = (RelativeLayout.LayoutParams) Layout.getLayoutParams();
scale = getResources().getDisplayMetrics().density;
DisplayMetrics dm = getResources().getDisplayMetrics();
height = dm.heightPixels;
width = dm.widthPixels;
}
//更新视频播放进度
private void progress(){
Log.d("这里是进度1",""+start1);
start1=videoView.getCurrentPosition();//现在的播放位置
if(total<=0){
total=videoView.getDuration();//总的播放长度
seekBar.setMax(total);
}
s1.setText(transformationDate(start1));
s2.setText(transformationDate(total));
seekBar.setProgress(start1);
}
//转换字符
private String changeString(int num){
return num >= 0 && num < 10 ? "0" + num : "" + num;
}
//转换时分秒函数,
private String transformationDate(int times){
//参数时间戳传入进来是以毫秒为单位的时间戳
//剩余秒数
int second=times/1000;
//剩余小时,每一个小时有60分钟,每分钟有60秒
int Time=second / 3600;
//剩余分钟,每分钟有60秒
second%=3600;//剩余的秒
int branch=second / 60;
//最后剩余秒数
second%=60;
return changeString(Time) + ":" + changeString(branch) + ":" + changeString(second);
}
@Override
protected void onDestroy() {
isPlay=true;
super.onDestroy();
}
//进入后台
@Override
protected void onPause() {
stop1();
videoView.pause();
handler1.removeMessages(1);
super.onPause();
}
//回到前台
@Override
protected void onResume() {
videoView.resume();
if(start1!=0){
handler1.removeMessages(1);
videoView.seekTo(start1);
}
super.onResume();
}
}
3.activity_main.xml内代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff"
>
<RelativeLayout
android:id="@+id/videoP"
android:layout_width="match_parent"
android:background="#000"
android:layout_height="200dp">
<VideoView
android:layout_centerHorizontal="true"
android:id="@+id/video1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/isShow"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#80000000"
>
<ImageView
android:layout_gravity="center_vertical"
android:id="@+id/play1"
android:src="@drawable/play"
android:layout_width="20dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/s1"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textColor="#fff"
android:layout_weight="1"
android:text=""/>
<SeekBar
android:id="@+id/SeekBar"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:maxHeight="5dp"
android:indeterminate="false"
android:progress="0"
android:layout_weight="6"
android:progressDrawable="@drawable/touch2"
android:thumb="@drawable/touch1"
/>
<TextView
android:id="@+id/s2"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textColor="#fff"
android:text=""/>
<ImageView
android:id="@+id/setMax"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:src="@drawable/max"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
4.touch1.xml内代码(touch1.xml和touch2.xml用于设置进度条SeekBar的样式)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/yuan"/>
<item android:state_pressed="false" android:drawable="@drawable/yuan"/>
</selector>
5.touch2.xml内代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#cccccc" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#ffffff" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#666666" />
</shape>
</clip>
</item>
</layer-list>