VideoView 播放本地、网络视频

通过VideoView播放本地、网络视频
知识点:
1.VideoView基本使用
2.访问内部存储
3.android 6.0动态获取权限
4.VideoView支持的视频类型


本文中一些方法使用,通过搜索资料得来。知道简单怎么用,具体原理还没研究后面有机会详细分析下


下面是代码:

public class MainActivity extends AppCompatActivity implements OnClickListener {

    Uri uri01;
    Uri uri02;
    Uri uri03;
    VideoView videoView;
    private static final String ACTIVITY_TAG = "MainActivity";
    private static final String[] PERMISSION = new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView = (VideoView) findViewById(R.id.videoView);
        videoView.setMediaController(new MediaController(this));

        findViewById(R.id.btn01).setOnClickListener(this);
        findViewById(R.id.btn02).setOnClickListener(this);
        findViewById(R.id.btn03).setOnClickListener(this);
        uri01 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test);
        //uri02 = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");//MP4视频
        //uri02 = Uri.parse("http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8");//m3u8视频
        uri02 = Uri.parse("http://live.3gv.ifeng.com/zixun.m3u8");//m3u8直播流,测试结果不支持直播流
        String sdcard = Environment.getExternalStorageDirectory().getPath() + "/Download/big.mp4";
        uri03 = Uri.parse(sdcard);
    }
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.btn01:
                Log.i(ACTIVITY_TAG,"btn01");
                Log.i(ACTIVITY_TAG,"按钮01");
                videoView.stopPlayback();
                videoView.setVideoURI(uri01);
                videoView.start();
                videoView.requestFocus();
                break;
            case R.id.btn02:
                Log.i(ACTIVITY_TAG,"btn02");
                Log.i(ACTIVITY_TAG,"按钮02");
                videoView.stopPlayback();
                videoView.setVideoPath(uri02.toString());
                videoView.start();
                videoView.requestFocus();
                break;
            case R.id.btn03:
                Log.i(ACTIVITY_TAG,"btn03");
                Log.i(ACTIVITY_TAG,"按钮03");
                isExist();

                //android 6.0以后需要动态获取权限,因为这个问题导致我之前一直无法播放本地视频。一开始使用的是在Manifest里加权限,但没有效果
                if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                    }else {
                        ActivityCompat.requestPermissions(this,PERMISSION,1);
                    }
                }
                videoView.stopPlayback();
                videoView.setVideoURI(uri03);
                videoView.start();
                videoView.requestFocus();
                break;
            default:
                break;
        }
    }

    private void isExist() {
//通过Environment.getExternalStorageDirectory().getPath()获取我也没搞清楚是SD卡还是内部存储  我是华为V8 没有外置SD卡
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/Download/big.mp4";
        File file = new File(filePath);
        if(file.exists()) {
            Log.i(ACTIVITY_TAG,filePath);
            Log.i(ACTIVITY_TAG,"文件存在");
//关于Log.i(),我每次如果只用一个Log.i(),在logcat里不显示,只有下面在写一次才会都显示
        } else {

            Log.i(ACTIVITY_TAG,filePath);
            Log.i(ACTIVITY_TAG,"不存在");
        }
    }
}

你可能感兴趣的:(VideoView 播放本地、网络视频)