Android provides the Videoview component, which works like ImageView, except that ImageView is used to display the picture, and Videoview is used to play the video.
Use Videoview the steps to play the video are as follows:
1) Define the Videoview component in the interface layout file, or create the Videoview component in the program.
2) Call Videoview The following two methods to load the specified video.
Setvideopath (String path): Loads the video represented by the path file.
Setvideouri (Uri uri): Loads the video corresponding to the URI.
3) Call Videoview's start (), Stop (), pause () method to control video playback.
In fact, in conjunction with videoview-, there is also a Mediacontroller class, which is used to provide a friendly graphical control interface to control the playback of the video through the control interface.
The following program demonstrates how to use Videoview to play a video:
public class Videoviewdemo extends Activity {videoview videoview; Create a Mediacontroller object to control the playback of the video Mediacontroller Mediacontroller; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); GetWindow (). SetFormat (pixelformat.translucent); Setcontentview (R.layout.main); Gets the Videoview component videoview= (videoview) Findviewbyid (R.id.videoview) on the interface; Initialize Mediacontroller mediacontroller=new Mediacontroller (this); The Association Videoview.setmediacontroller (Mediacontroller) was established with Videoview and Mediacontroller; The Association Mediacontroller.setmediaplayer (Videoview) was established with Mediacontroller and Videoview; File Vfile=new file ("/mnt/sdcard/meitianyifenzhong.3gp"); if (vfile.exists ()) {//If the file exists VideovieW.setvideopath (Vfile.getabsolutepath ()); Let Videoview get focus videoview.requestfocus (); } }}
Layout interface:
<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 " > <videoview android:id= "@+id/videoview" android:layout_width= "Match_parent"
Program run:
: The fast forward key, pause key, back key, and the playback progress bar in the interface are provided by Mediaplayercontroller.
hint: Videoview can only play standard MP4, 3GP files, files in other formats will not play.