Video playback using VideoView in Android Development

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.