使用VideoView播放视频

  1.  布局文件

?
1
2
3
4
< VideoView
     android:id = "@+id/video"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"  />

2.  安卓代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public  class  MainActivity  extends  AppCompatActivity {
     private  VideoView videoView=  null ;
     private  MediaController mediaController;
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         getWindow().setFormat(PixelFormat.TRANSLUCENT);
         setContentView(R.layout.activity_main);
         videoView = (VideoView)findViewById(R.id.video);
         mediaController =  new  MediaController( this );
         File video =  new  File( "/mnt/usb/sda1/test.mp4" );
         if (video.exists()) {
             videoView.setVideoPath(video.getAbsolutePath());
             videoView.setMediaController(mediaController);
             mediaController.setMediaPlayer(videoView);
             videoView.requestFocus();
         }
     }
}

3. 在AndroidMainFest.xml中添加

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

你可能感兴趣的:(android,视频,播放器,VideoView)