First, the layout file:
1 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " 2 xmlns:tools=" Http://schemas.android.com/tools " 3 android:layout_width=" match_parent " 4 android:layout_height= "Match_parent" 5 android:orientation= "vertical" 6 tools:context= ". Mainactivity "> 7 8 <videoview 9 android:id=" @+id/video "10 android:layout_width=" match_parent "11 android:layou t_height= "Match_parent"/>12 13 </ Linearlayout>
Then use the Videoview in mainactivity:
1 ImportJava.io.File;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 Importandroid.os.Environment;6 ImportAndroid.widget.MediaController;7 ImportAndroid.widget.Toast;8 ImportAndroid.widget.VideoView;9 Ten Public classMainactivityextendsActivity { One Videoview Videoview; A Mediacontroller Mcontroller; - - @Override the protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Videoview =(Videoview) Findviewbyid (r.id.video); +Mcontroller =NewMediacontroller ( This); -File video =NewFile (Environment.getexternalstoragedirectory () + "/xiexienideai.mp4");//get the SD card video storage path on your phone + if(Video.exists ()) { A Videoview.setvideopath (Video.getabsolutepath ()); atVideoview.setmediacontroller (Mcontroller);//Set Mcontroller Association Videoview - Mcontroller.setmediaplayer (videoview); -Videoview.requestfocus ();//Videoview Get Focus -}Else{ -Toast.maketext ( This, "NoNoNo", Toast.length_long). Show (); - } in } - to}
Due to the use of content acquisition on the SD card, you need to add permissions in the configuration file:
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
Use Videoview to play video in Android