视频播放结合进度条

package alice.bw.com.day03surfaceviewmedaplayerseekbar3;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
private SurfaceView surfaceView;
private SeekBar seekBar;
private SurfaceHolder holder;
private MediaPlayer player;

//更改进度的广播
private ProgressReceiver receiver;
private boolean flag = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}

//初始化 各种组件
private void init() {
    // 实例化  播放器
    player = MediaPlayer.create(this, R.raw.a);
    // surfaceView
    surfaceView = ((SurfaceView) findViewById(R.id.surfaceView));
    //进度条
    seekBar = ((SeekBar) findViewById(R.id.seekBar));
    // 广播接收者
    receiver = new ProgressReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("alice.com.player");
    //动态注册
    registerReceiver(receiver, filter);

    //surfaceHolder
    holder = surfaceView.getHolder();
    holder.addCallback(this);
    // 固定surfaceView的屏幕大小
    holder.setKeepScreenOn(true);
    //SURFACE_TYPE_PUSH_BUFFERS表明该Surface不包含原生数据,
    // Surface用到的数据由其他对象提供,在Camera图像预览中就使用该类型的Surface,
    // 有Camera负责提供给预览Surface数据,这样图像预览会比较流畅。
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    // 给seekBar 设置一个最大值 , 最大值是  MediaPlayer的 长度
    seekBar.setMax(player.getDuration());
    seekBar.setProgress(0);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        /*
        参数二: 代表当前seekBar滑动的进度
        参数三:  true 表示手指拖拽    false 表示 设置进度
        当拖动条发生变化时调用该方法
         */
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if (b)
                player.seekTo(i);
        }

        //当用户开始滑动滑块时调用该方法(即按下鼠调用一次)
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            Log.d("sxl", "onStartTrackingTouch: ");
            player.pause();
        }

        //当用户结束对滑块滑动时,调用该方法(即松开鼠标)
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            Log.d("sxl", "onStopTrackingTouch: ");
            player.start();

        }
    });

}

public void onClick(View view) {

    switch (view.getId()) {
        case R.id.play_bt:
            // 开始播放
            if (player != null && !player.isPlaying())
                player.start();

            break;
        case R.id.pause_bt:
            if (player != null && player.isPlaying())
                player.pause();
            break;
    }
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    // 设置  显示 View
    if (player != null)
        player.setDisplay(surfaceHolder);//
    // 开启 记录时间的 线程
    new MyThread().start();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

}


class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        Intent intent = new Intent();
        intent.setAction("alice.com.player");
        while (flag) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (player != null) {
                // 发送广播  更新 seekBar  当前进度
                int currentPosition = player.getCurrentPosition();
                intent.putExtra("progress", currentPosition);
                sendBroadcast(intent);
            }
        }
    }
}

class ProgressReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int progress = intent.getIntExtra("progress", 0);
        //接收进度 并更新  seekBar
        seekBar.setProgress(progress);
    }
}


// 销毁的时候需要处理
@Override
protected void onDestroy() {
    super.onDestroy();
    flag = false;
    unregisterReceiver(receiver);
    if (player != null) {
        player.release();
        player = null;
    }
}

}

发布了30 篇原创文章 · 获赞 4 · 访问量 7276
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览