VideoView学习笔记

来源:互联网 发布:ubc专业 知乎 编辑:程序博客网 时间:2024/06/06 12:49

(一)、使用要点(步骤)

首先要做xml布局文件中写视频播放的宽高

<VideoView            android:id="@+id/video_view"            android:layout_width="match_parent"            android:layout_height="match_parent"/>

1.通过videoView.setVideoURI(Uri.parse(file.getAbsolutePath()));设置要播放的数据源
2.可以使用系统自带的控制栏,创建方式如下:
MediaController mediaController = new MediaController(this);
3.使用系统自带的控制栏时需要将VideoView和控制栏进行双向绑定。示例如下:

//将MediaController和VideoView绑定到一起videoView.setMediaController(mediaController);//双向绑定,将VideoView绑定到MediaControllermediaController.setMediaPlayer(videoView);

4.可以根据业务需求调整控制栏的位置:

        //开始播放        videoView.start();        //暂停播放        videoView.pause();        //停止播放        videoView.stopPlayback()
//设置MediaController的左上右下的位置,但是只有一个起作用mediaController.setPadding(0,0,0,200);

(二)、布局代码

首先有一个视频的截图当封面,有一个播放按钮,按下就播放

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="org.mobiletrain.a8_4videoview.MainActivity">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="200dp">        <VideoView            android:id="@+id/video_view"            android:layout_width="match_parent"            android:layout_height="match_parent"/>        <ImageView            android:id="@+id/iv"            android:layout_width="match_parent"            android:layout_height="match_parent"/>    </FrameLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="play"        android:text="播放"/></LinearLayout>

(三)、示例代码

 public class MainActivity extends AppCompatActivity {    private VideoView videoView;    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        videoView = (VideoView) findViewById(R.id.video_view);        iv = (ImageView) findViewById(R.id.iv);        //找到手机DOWNLOADS路径下的名字为“曾经的你.mp4”的文件,如果没有就是NULL        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "曾经的你.mp4");        //getThumbnailBitmap()这是自定义的方法        Bitmap bitmap = getThumbnailBitmap(file);        iv.setImageBitmap(bitmap);        Uri uri = Uri.parse(file.getAbsolutePath());        //设置播放的数据源        videoView.setVideoURI(uri);        MediaController controller = new MediaController(this);        videoView.setMediaController(controller);        //设置MediaController的位置//        videoView.setPadding();    }    /**     * 获取视频文件的缩略图     * @param file  视频文件     * @return     */    private Bitmap getThumbnailBitmap(File file) {        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();        Bitmap bitmap;        try {            //设置数据源为获得缩略图做准备            mediaMetadataRetriever.setDataSource(this, Uri.parse(file.getAbsolutePath()));            //获取视频文件的任意一帧作为该视频文件的缩略图            bitmap = mediaMetadataRetriever.getFrameAtTime();        } finally {            //释放资源            mediaMetadataRetriever.release();        }        return bitmap;    }    public void play(View view) {        //开始播放        videoView.start();        //暂停播放//        videoView.pause();        //停止播放//        videoView.stopPlayback();//这个是隐藏封面图片        iv.setVisibility(View.GONE);    }}

第二个例子

上个例子是系统自带的进度条和总时间,当时间。这里我们自定义

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="org.mobiletrain.a8_4videoview2.MainActivity">    <VideoView        android:id="@+id/video_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="48dp"        android:layout_alignBottom="@id/video_view">//目前的时间        <TextView            android:id="@+id/currentTimeTv"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginLeft="12dp"            android:gravity="center"            android:text="00:00"            android:textColor="#ffffff"/>//这个是总共的时间        <TextView            android:id="@+id/totalTimeTv"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_alignParentRight="true"            android:layout_marginRight="12dp"            android:gravity="center"            android:text="00:00"            android:textColor="#ffffff"/>        <SeekBar            android:id="@+id/seekbar"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_centerVertical="true"            android:layout_toLeftOf="@id/totalTimeTv"            android:layout_toRightOf="@id/currentTimeTv"/>    </RelativeLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/video_view"        android:onClick="play"        android:text="播放"/></RelativeLayout>

下面是MianActivity.java中的代码

public class MainActivity extends AppCompatActivity {    private VideoView videoView;    private TextView currentTimeTv;    private TextView totalTimeTv;    private boolean isPlaying = false;    //定义了显示时间的模式mm:ss,即是分钟:秒,自己去看jdk文档    private SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");    private SeekBar seekBar;    private Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            if (isPlaying) { //延时1000毫秒,再发送一次空数据,使在Handler中不断循环                mHandler.sendEmptyMessageDelayed(0, 1000);                Log.d("google_lenve_fb", "handleMessage: ++++++++++");                currentTimeTv.setText(dateFormat.format(new Date(videoView.getCurrentPosition())));//设置进度                seekBar.setProgress(videoView.getCurrentPosition());            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        videoView = (VideoView) findViewById(R.id.video_view);        currentTimeTv = (TextView) findViewById(R.id.currentTimeTv);        totalTimeTv = (TextView) findViewById(R.id.totalTimeTv);        seekBar = (SeekBar) findViewById(R.id.seekbar);        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "曾经的你.mp4");        Log.d("google_lenve_fb", "onCreate: " + file.getAbsolutePath());      //注意        videoView.setVideoURI(Uri.parse(file.getAbsolutePath()));        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {            //如果Seekbar的改变是由用户手动拖动导致的,则fromUser为true            @Override            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {                Log.d("google_lenve_fb", "onProgressChanged: " + progress);   //判断进度条的变化是用户拖动的,还是自己SeekBar.setProgress()设定的                if (fromUser) {                //设置播放进度                    videoView.seekTo(progress);                }            }            @Override            public void onStartTrackingTouch(SeekBar seekBar) {                videoView.pause();            }            @Override            public void onStopTrackingTouch(SeekBar seekBar) {                videoView.start();            }        });    }    public void play(View view) {        videoView.start();        //videoView.getDuration();只有VideoView开始播放的时候才可以获取到视频文件的总长度        totalTimeTv.setText(dateFormat.format(new Date(videoView.getDuration())));        seekBar.setMax(videoView.getDuration());        isPlaying = true;        mHandler.sendEmptyMessage(0);    }    @Override    protected void onDestroy() {        super.onDestroy();        //最后销毁的是记得        isPlaying = false;    }}
videoView.getCurrentPosition();//后去当前播放的位置        //开始播放        videoView.start();        //暂停播放        videoView.pause();        //停止播放        videoView.stopPlayback();        //设置播放进度        videoView.seekTo(progress);
0 0
原创粉丝点击