Video playback using VideoView in Android Development
Android provides the VideoView component, which is similar to ImageView, but ImageView is used to display images while VideoView is used to play videos.
To use VideoView to play a video, follow these steps:
1) define the VideoView component in the interface layout file or create a VideoView component in the program.
2) call the following two methods of VideoView to load the specified video.
SetVideoPath (String path): loads the video represented by the path file.
SetVideoURI: loads the video corresponding to the Uri.
3) Call the start (), stop (), and pause () Methods of VideoView to control video playback.
In fact, a MediaController class is used in combination with VideoView. It provides a friendly graphic control interface that controls video playback.
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 video playback MediaController mediaController; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getWindow (). setFormat (PixelFormat. TRANSLUCENT); setContentView (R. layout. main); // obtain the VideoView component videoView = (VideoView) findViewById (R. id. videoView); // initialize mediaController = new MediaController (this); // associate videoView with mediaController. setMediaController (mediaController); // associate mediaController with videoView. setMediaPlayer (videoView); File vFile = new File ("/mnt/sdcard/meitianyifenzhong.3gp"); if (vFile. exists () {// if the file has videoView. setVideoPath (vFile. getAbsolutePath (); // obtain the videoView focus. requestFocus ();}}}
Layout interface:
Program running:
: The shortcut key, shortcut key, backend key, and playback progress bar are provided by MediaPlayerController.
Tip: VideoView can only play standard MP4 and 3GP files. Files in other formats cannot be played.