In Android, you can play a video in three ways: the first method is to use mediaplayer and surfaceview to play the video, and the mediaplayer is used to control the playback, pause, and progress of the video, surfaceview is used to display the video content. The second method is to use videoview for playback. This class actually inherits the surfaceview class and implements mediacontroller. mediaplayercontroller is an interface used to control media playback. In addition, it also has a panel on videoview for controlling media playback, including fast forward, fast return, play, pause button, and a progress bar. The advantage of using videoview to play a video is that it is simple because it has helped us implement surfaceview and control methods. We only need to use it directly, but its disadvantage is that it is not flexible enough. Mediaplayer can be used in combination with surfaceview to play a video. The advantage is that it can be customized more flexibly, but the disadvantage is that it is more difficult. This time, we will first look at how to use videoview to play the video. : The third method is to use its own player. Specify action as action_view, data as Uri, and type as its MIME type.
Common Methods for vidoeview are as follows:
In addition, mediacontroller also provides some useful methods, as shown in the following table:
Here is an example.
First, we need to define a layout. In this layout, we need a videoview component. The file is as follows:
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: layout_height = "match_parent">
A videoview is defined here, and its ID is videoview1. Then, we will compile the corresponding activity:
Package cn.com. farsight. VV;
Import Android. App. activity;
Import Android. content. PM. activityinfo;
Import Android. OS. Bundle;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. widget. mediacontroller;
Import Android. widget. videoview;
Public class mainactivity extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Remove the header title
Requestwindowfeature (window. feature_no_title );
// Set full screen
Setrequestedorientation (activityinfo. screen_orientation_landscape );
// Set the screen to always bright
Getwindow (). addflags (windowmanager. layoutparams. flag_keep_screen_on );
Setcontentview (R. layout. Main );
Videoview VV = (videoview) findviewbyid (R. Id. videoview1 );
Vv. setvideopath ("/mnt/sdcard/a5.mp4 ");
// Set the media control bar
Vv. setmediacontroller (New mediacontroller (this ));
Vv. Start ();
Vv. requestfocus ();
}
}
In this way, you can play the video file/mnt/sdcard/a5.mp4.
In addition, to prevent the user from mistakenly pressing the return key and exiting the playback, You can overwrite the onbackpressed method in this program to process the user's clicking back key action, for example, you can only click the return key twice in a short time to exit playback.