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