VideoView实现全屏和横屏播放

来源:互联网 发布:腾讯视频网络不稳定 编辑:程序博客网 时间:2024/06/07 17:04

1)XML布局

         <VideoView>控件的使用:

在布局的时候,VideoView要使用一个控件包裹起来,我这里使用的是id= Video_play_quanRelativeLayout控件。

<RelativeLayoutxmlns: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"
>
   
    <RelativeLayout
       
android:id="@+id/Video_play_quan"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
>

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

    </RelativeLayout>

</RelativeLayout>

2)java代码实现

   public voidplayVideo(String path){
//     String path =Environment.getExternalStorageDirectory().getPath() + File.separator +"test.mp4";
       
videoView.setVideoURI(Uri.parse(path));

        MediaController mctrl = new MediaController(this);
        videoView.setMediaController(mctrl);

        RelativeLayout.LayoutParamslayoutParams = newRelativeLayout.LayoutParams(
               RelativeLayout.LayoutParams.FILL_PARENT,
               RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
       layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
       layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
       layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        videoView.setLayoutParams(layoutParams);

        videoView.start();
        videoView.requestFocus();
    }

0 0