Android Development using Videoview play video

Source: Internet
Author: User

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.

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.