SurefaceView+MediaPlayer 视频播放器

效果展示先:

在这里插入图片描述
基本功能都是有的,前进后退暂停进度条等都有

代码展示

布局文件:

在这里插入图片描述
在这里插入图片描述

代码展示

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();//释放资源
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值