MediaPlayer+SurfaceView视频播放器

MediaPlayer+SurfaceView视频播放器

    • 使用方法

使用方法

MediaPlayer的主要功能是运行MP3、MP4等文件,MP3文件可直接运行(之前博客有发过),MP4则需要搭配SurfaceView使用。
具体用法:
首先我们需要将完成布局,初始化控件。
然后将我们获得的SurfaceHolder对象通过MediaPlayer的setDisplay()方法绑定。
最终我们通过一些功能的添加可以实现更完美的视频播放器。

具体代码如下:

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{

    SurfaceView surfaceView;
    MediaPlayer player;
    CheckBox checkBox;
    SurfaceHolder holder;
    ProgressBar progressBar;
    SeekBar seekBar;
    TextView time;
    TextView sumtime;
    Timer timer=new Timer();
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int a = msg.arg1;
            setTimeNow(a);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        player=new MediaPlayer();
        surfaceView=findViewById(R.id.surfaceView);
        progressBar=findViewById(R.id.jidutiao);
        seekBar=findViewById(R.id.seek);
        time=findViewById(R.id.timeing);
        sumtime=findViewById(R.id.sumtime);

        progressBar.setVisibility(View.VISIBLE);
        checkBox=findViewById(R.id.bofang);
        checkBox.setChecked(false);
        checkBox.setEnabled(false);
        holder=surfaceView.getHolder();
        holder.addCallback(this);


        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                progressBar.setVisibility(View.GONE);
                player.setLooping(true);
                int duration = player.getDuration();
                String time1 = getTime(duration);
                sumtime.setText(time1);
                setTimeNow(0);
                seekBar.setMax(duration);
                checkBox.setEnabled(true);
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if(player.getDuration()==player.getCurrentPosition()){
                            seekBar.setProgress(0);
                        }else {
                            seekBar.setProgress(player.getCurrentPosition());
                        }
                        Message obtain = Message.obtain();
                        obtain.arg1=player.getCurrentPosition();
                        handler.sendMessage(obtain);
                    }
                },0,500);

            }
        });




        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if(fromUser){
                        player.seekTo(progress);
                    }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d("###", "onCheckedChanged: "+isChecked);
                if(isChecked){
                    player.start();
                }else{
                    player.pause();
                }
                checkBox.setChecked(isChecked);
            }
        });
    }

    public String getTime(long l){
        long sum = l / 1000;
        int  f= (int) (sum/60);
        int  m = (int) (sum%60);
        String f1 = f+"";
        String f2 = m+"";
        if (f<10){
            f1 = 0+""+f;
        }
        if (m<10){
            f2 = 0+""+m;
        }
        return f1+":"+f2;
    }

    public void setTimeNow(long l){
        long sum = l / 1000;
        int  f= (int) (sum/60);
        int  m = (int) (sum%60);
        String f1 = f+"";
        String f2 = m+"";
        if (f<10){
            f1 = 0+""+f;
        }
        if (m<10){
            f2 = 0+""+m;
        }
        time.setText(f1+":"+f2);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
        try {
            player.setDataSource(MainActivity.this, uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.setDisplay(holder);
        player.prepareAsync();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        player.stop();
        player.release();
    }
}

效果实现:
MediaPlayer+SurfaceView视频播放器_第1张图片

你可能感兴趣的:(Android)