Play Video using Videoview

Source: Internet
Author: User

To play video in Android apps, Android offers the Videoview component, It is a component that is located under the Android.widget package, which functions like ImageView, except that ImageView is used to display the image and Videoview to play the video.

To play a video using Videoview, follow these steps:

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); load uri corresponding video '

3. Call Videoview's start (), Stop (), pause () method to control video playback.

Using Videoview, it is also necessary to combine the Mediacontroller class, which is to provide a friendly graphical control interface to control the video playback through this control interface.

The Videoview interface layout file is as follows:

<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"
android:orientation= "Vertical"
Tools:context= ". Videoviewtest ">

<!--definition Videoview play video--
<videoview
Android:id= "@+id/video"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
/>

</LinearLayout>

The program code is as follows:

Import Java.io.File;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.graphics.PixelFormat;
Import Android.view.Menu;
Import Android.widget.MediaController;
Import Android.widget.VideoView;

public class Videoviewtest extends Activity {
Videoview Videoview;
Mediacontroller Mcontroller;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
GetWindow (). SetFormat (pixelformat.translucent);
Setcontentview (r.layout.activity_video_view_test);
Get the Videoview component on the interface
Videoview = (videoview) Findviewbyid (R.id.video);
Create a Mediacontroller object
Mcontroller = new Mediacontroller (this);
File Video = new file ("/mnt/sdcard/movie.mp4");
if (video.exists ()) {
Videoview.setvideopath (Video.getabsolutepath ());
Setting up Videoview to associate with Mediacontroller
Videoview.setmediacontroller (Mcontroller);
Setting up Mediacontroller to associate with Videoview
Mcontroller.setmediaplayer (Videoview);
Let Videoview get the focus
Videoview.requestfocus ();
}
}

}

Running the program may encounter some problems, such as the use of some non-standard MP4, 3GP files, then the application will not play, it is recommended to use the mobile phone to record a compatible with a variety of mobile phones, standard MP4, 3GP video files.

Play Video using Videoview

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.