We can imagine ImageView can display pictures, and Videoview is used to show the video
To play a video using Videoview, follow these steps
"1" defines videoview in the interface layout
<linearlayout 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"android:orientation="Vertical" Tools:context=". Mainactivity "> <videoview android:id = "@+id/videoview" android:layout_width = "match_parent" android:layout_height = "0DP" android:layout_weight = "1" /> <buttonandroid:id="@+id/button"android:text="Play" Android:layout_width="Wrap_content"android:layout_height="Wrap_content" /> </linearlayout>
"2" invokes the following two methods to load the specified video
Setvideopath (String path), video under load path
Setvideourl (URL URL); Loads the video corresponding to the URL.
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");
"3" permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
"4" Call
Start (), Stop (), pause () control playback
"5" In practice often combines the Mediacontroller class, which provides a friendly image control interface to control video playback;
mVideoView.setMediaController(new MediaController(MainActivity.this));
The complete program code is as follows
Public class mainactivity extends Activity { PrivateVideoview Mvideoview;PrivateButton Mbutton;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); mvideoview= (Videoview) Findviewbyid (R.id.videoview); mbutton= (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {//Get sdcard aa.mp4 video files below //Two ways to call//File videofile =new file ("/mut/extsdcard/dcim/camera/20150915_160202.mp4");//Mvideoview.setvideopath (Videofile.getabsolutepath ());Mvideoview.setvideopath (environment.getexternalstoragedirectory () +"/20150915_160202.mp4"); Mvideoview.setmediacontroller (NewMediacontroller (mainactivity. This)); Mvideoview.start (); } }); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
5-Step learning to play video with Videoview