1. 程式人生 > >VideoView實現全屏和橫屏播放

VideoView實現全屏和橫屏播放

1)XML佈局

         <VideoView>控制元件的使用:

在佈局的時候,VideoView要使用一個控制元件包裹起來,我這裡使用的是id= Video_play_quanRelativeLayout控制元件。

<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">
   
    <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 void playVideo(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 = new RelativeLayout.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();
    }