自定义简单视频播放器

效果图如下:
自定义简单视频播放器_第1张图片

以下是代码:

package com.example.android_27__playvideo_camera_lx;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.sb_main_bar);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);


        //给进度条设置滑动的监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress=seekBar.getProgress();
                //在当前位置播放
                mediaPlayer.seekTo(progress);
            }
        });
    }

    public void isPlayOrPause(View view){
        final ImageButton imageButton = (ImageButton) view;
        if(mediaPlayer==null){
            //播放内存卡中的文件
            mediaPlayer = new MediaPlayer();
            //设置音乐来源路劲
            try {
                //内存卡路劲,要加上file
                mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/mp/sister.mp4"));
                //准备
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }

            //将媒体播放器捕捉到的画面显示到surfaceview
            mediaPlayer.setDisplay(surfaceView.getHolder());
            //开始播放
            mediaPlayer.start();
            //把播放图标换成暂停图标
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
                    //获取音乐的总时长
                    int pd = mediaPlayer.getDuration();
                    //将进度条设置最大值为:音乐的总时长
                    seekBar.setMax(pd);
                    //实例化线程
                    new myThread().start();
        }else if(mediaPlayer.isPlaying()){//判断是否正在播放
            //暂停播放
            mediaPlayer.pause();
            imageButton.setImageResource(android.R.drawable.ic_media_play);
        }else {
            //开始播放
            mediaPlayer.start();
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
        }
    }

    class myThread extends Thread{
        @Override
        public void run() {
            super.run();
            while (seekBar.getProgress()<=seekBar.getMax()){
                //获取音乐当前播放的位置
                int cp = mediaPlayer.getCurrentPosition();
                seekBar.setProgress(cp);
            }

        }
    }

}

注意要给权限:

 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

你可能感兴趣的:(视频)