效果展示先:
基本功能都是有的,前进后退暂停进度条等都有
代码展示
布局文件:
代码展示
public class Main12Activity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, View.OnClickListener, SurfaceHolder.Callback {
SurfaceView sur;
SurfaceHolder holder;
SeekBar bar;
Button up;
Button after;
Button start_stop;
TextView dang;
TextView sum;
int time;
MediaPlayer player;
boolean isPause;
boolean isStart;
int times;
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
Date date = new Date();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main12);
sur = findViewById(R.id.sur);
holder = sur.getHolder();
holder.addCallback(this);
bar = findViewById(R.id.bar);
up = findViewById(R.id.ups);
after = findViewById(R.id.after);
start_stop = findViewById(R.id.start_stop);
dang = findViewById(R.id.dang);
sum = findViewById(R.id.sum);
bar.setOnSeekBarChangeListener(this);
up.setOnClickListener(this);
start_stop.setOnClickListener(this);
after.setOnClickListener(this);
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//判断进度条是否继续
if(time*1000 <= times && !isPause) {
bar.setProgress(time * 1000);
date.setTime(time * 1000);
String format = sdf.format(date);
dang.setText(format);
Message message = new Message();
time++;
handler.sendMessageDelayed(message, 1000);
}else if(time*1000 >= times){
time = 0;
bar.setProgress(time * 1000);
date.setTime(time * 1000);
String format = sdf.format(date);
dang.setText(format);
Message message = new Message();
time++;
handler.sendMessageDelayed(message, 1000);
}
}
};
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
player.seekTo(seekBar.getProgress());//设置mediaplayer的播放进度
handler.removeCallbacksAndMessages(null);//删除所有handler的信息.
Message message = new Message();
time = seekBar.getProgress()/1000;
handler.sendMessage(message);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ups://进度后退5秒钟
if(time >= 5){
time-=5;
}else{
time = 0;
}
player.seekTo(time*1000);//设置mediaplayer的播放进度
handler.removeCallbacksAndMessages(null);//删除所有handler的信息.
Message message = new Message();
handler.sendMessage(message);
break;
case R.id.start_stop://暂停及开始
if(isPause){
player.start();
isPause = false;
start_stop.setBackgroundResource(R.mipmap.start);
}else{
player.pause();
isPause = true;
start_stop.setBackgroundResource(R.mipmap.stop);
}
Message message1 = new Message();
handler.sendMessage(message1);
break;
case R.id.after://进度条前进5秒钟
if(time+5 <= times){
time+=5;
}else{
time = times;
}
player.seekTo(time*1000);//设置mediaplayer的播放进度
handler.removeCallbacksAndMessages(null);//删除所有handler的信息.
Message mes = new Message();
handler.sendMessage(mes);
break;
default:
break;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
newMideaPlear();
player.setDisplay(holder);//设置让surfaceview来播放视频.
player.prepareAsync();//异步准备.
}
private void newMideaPlear() {
player = new MediaPlayer();
try {
player.setDataSource("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4");
} catch (IOException e) {
e.printStackTrace();
}
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.setLooping(true);//设置循环播放
times = player.getDuration();//获取视频长度
int jin = times;
date.setTime(jin);
String format = sdf.format(date);
sum.setText(format);
bar.setMax(times);//设置进度条最大值
player.start();//开始播放
Message message = new Message();
message.arg1 = time;
handler.sendMessage(message);
isStart = true;
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
player.stop();//停止播放
player.release();//释放资源
}
}