android中VideoView播放sd卡上面的视频

(1)videoView组件只支持MP4和3gp格式的视屏播放,如果想播放其它视屏格式的文件,还得开发能够播放的视屏播放器

(2)videoView组件功能比较单一,如果想开发功能丰富的播放器,还得重写VideoView组件

(3)videoView的基本用法,首先在布局文件里面添加videoView组件,具体的布局依据自己的需求,由于我这里要用到全屏,所以我重写了videoView组件

package com.wxyz.dengchaoqun.testswf;

import android.content.Context;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.VideoView;

/**
 * Created by 邓超群 on 2017/2/4.
 */

public class FullScreenVideoView extends VideoView {
    public FullScreenVideoView(Context context) {
        super(context);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        setMeasuredDimension(width, height);
    }
}

然后再需要调用的Activity中去使用该组件,用法如下

 videoView=(VideoView)findViewById(R.id.videoView);
        File file=new File("/sdcard/aaa.mp4");
        MediaController mc=new MediaController(MainActivity.this);
        if(file.exists()){
            videoView.setVideoPath(file.getAbsolutePath());
            videoView.setMediaController(mc);
            videoView.requestFocus();
            try {
                videoView.start();
            }catch (Exception e){
                e.printStackTrace();
            }
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    Toast.makeText(MainActivity.this,"视频播放完毕",Toast.LENGTH_SHORT).show();
                }
            });
        }else{
            Toast.makeText(MainActivity.this,"要播放的视屏文件不存在",Toast.LENGTH_SHORT).show();
        }

 (4)要访问sd上的文件是需要权限的,在配置文件中添加权限

<!--访问sd卡权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

  

以上就是VideoView组件的基础用法,有兴趣可以更加深入的研究

原文地址:https://www.cnblogs.com/deng-c-q/p/6366117.html