Android provides the Videoview component. It is similar in function to ImageView, only ImageView is used to display pictures. Instead, Videoview is used to play the video.
Use Videoview the process of playing the video is as follows:
1) Define the Videoview component in the interface layout file, or create the Videoview component in the program.
2) Call Videoview For example the following two methods to load a 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.
Another Mediacontroller class that is actually used in conjunction with videoview-. Its role is to provide a friendly graphical control interface to control the playback of video through this 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 ()) {//Assuming 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 execution:
What you see: The Fast forward key, pause key, back key, and playback progress bar in the interface are provided by Mediaplayercontroller.
Tips: Videoview can only play the standard MP4, 3GP files. Files in other formats will not play.
Android Development using Videoview play video