Two ways to play Video:
Play video with Videoview (convenient, recommended)
Play video using MediaPlayer and Surfaceview (early way)
The first way:
The process of using Videoview to play video is as follows:
Define the Videoview component in the interface layout file, or create a Videoview build in the program
Call Videoview For example the following two methods to load a specified video
Setvideopath (String paht): Loads the video represented by the path file
Setvideouri (URI uri): load the corresponding video for the URI
Call Videoview's start (), Stop (), pause () method to control video playback
Mediacontraller class
Another Mediacontraller class, used in conjunction with Videoview, provides a friendly graphical control interface to control video playback, Fast forward keys, pause keys, back keys, and playback progress.
are provided by this class
Sample code:
XML layout:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent " ><!--definition videoview play video--><videoviewandroid:id= "@+id/video" android:layout_width= "Match_parent" Android : layout_height= "Match_parent"/></linearlayout>
Activity:
Import Java.io.file;import android.app.activity;import android.graphics.pixelformat;import android.os.Bundle; Import Android.widget.mediacontroller;import Android.widget.videoview;public class Vedioviewtest extends Activity {Vi Deoview Videoview; Mediacontroller Mcontroller; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); GetWindow (). SetFormat (pixelformat.translucent); Setcontentview (R.layout.main); Get interface on Videoview component Videoview = (videoview) Findviewbyid (R.id.video); Create Mediacontroller Object mcontroller = new Mediacontroller (this); File Video = new file ("/mnt/sdcard/movie.mp4"); if (video.exists ()) {Videoview.setvideopath (Video.getabsolutepath ());//①//set Videoview and Mcontrolle R Establish Association Videoview.setmediacontroller (Mcontroller); ②//set Mcontroller with Videoview to establish Association Mcontroller.setmediaplayer (Videoview); ③ Let Videoview get focus videoview.requestfocus (); } }}
Another way:
The process of using MediaPlayer to play video is as follows:
Create a MediaPlayer object and let it load the specified video file
Define the Surfaceview component in the interface layout file, or create the Surfaceview component in the program, and add Surfaceholder listeners for the callback of Surfaceview Surfaceview
Call the MediaPlayer object's Setpisplay (Surfaceholder sh): Outputs the video image that is played to the specified Surfaceview component
Call the start (), Stop (), and Pause () methods of the MediaPlayer object to control the playback of the video
Sample code:
XML layout:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent " ><surfaceviewandroid:id= "@+id/surfaceview" android:layout_width= "Match_parent" android:layout_height= " 360DP "/><linearlayoutandroid:orientation=" horizontal "android:layout_width=" Match_parent "Android:layout_ height= "Wrap_content" android:layout_alignparentbottom= "true" android:gravity= "Center_horizontal" >< Imagebuttonandroid:id= "@+id/play" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/play"/><imagebutton android:id= "@+id/pause" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/pause"/><imagebuttonandroid:id= "@+id/stop" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/stOp "/></linearlayout></relativelayout>
Activity:
Import Java.io.ioexception;import Android.app.activity;import Android.media.audiomanager;import Android.media.mediaplayer;import Android.os.bundle;import Android.util.displaymetrics;import Android.view.surfaceholder;import Android.view.surfaceview;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.windowmanager;import Android.widget.relativelayout.layoutparams;import Android.widget.imagebutton;public class SurfaceViewPlayVideo Extends Activityimplements Onclicklistener{surfaceview Surfaceview;imagebutton play, pause, stop; MediaPlayer mplayer;//Record the playback position of the current video int position; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Get 3 buttonplay in the interface = (ImageButton) Findviewbyid (r.id.play);p ause = (ImageButton) Findviewbyid (r.id.pause); stop = (ImageButton) Findviewbyid (r.id.stop) ;//The Click event Binding Event Listener Play.setonclicklistener (this) for 3 buttons;p Ause.setonclicklistener (this); Stop.setonclicklistener ( This;//Create Mediaplayermplayer = new MediaPlayer (); Surfaceview = (Surfaceview) This.findviewbyid (R.id.surfaceview);// Set playback to open the screen Surfaceview.getholder (). Setkeepscreenon (True); Surfaceview.getholder (). Addcallback (New Surfacelistener ( ));} @Overridepublic void OnClick (View source) {Try{switch (Source.getid ()) {//Play button is clicked Case R.id.play:play (); break;// Pause button is clicked Case R.id.pause:if (Mplayer.isplaying ()) {Mplayer.pause ();} Else{mplayer.start ();} break;//stop button is clicked Case R.id.stop:if (Mplayer.isplaying ()) mplayer.stop (); catch (Exception e) {e.printstacktrace ();}} private void Play () throws Ioexception{mplayer.reset (); Mplayer.setaudiostreamtype (audiomanager.stream_music);// Set the video Mplayer.setdatasource ("/mnt/sdcard/movie.3gp") you need to play, and//output the video to Surfaceviewmplayer.setdisplay ( Surfaceview.getholder ()); ①mplayer.prepare ();//Get the form manager WindowManager Wmanager = Getwindowmanager ();D isplaymetrics metrics = new Displaymetrics ();//Gets the screen size Wmanager.getdefaultdisplay (). Getmetrics (metrics);//set video to keep aspect ratio zoom to full screen SURFACEview.setlayoutparams (New Layoutparams (Metrics.widthpixels, Mplayer.getvideoheight () * metrics.widthpixels/ Mplayer.getvideowidth ())); Mplayer.start ();} Private class Surfacelistener implements surfaceholder.callback{@Overridepublic void surfacechanged (Surfaceholder Holder, int format,int width, int height) {} @Overridepublic void surfacecreated (Surfaceholder holder) {if (Position > 0) {try{//starts playing play ();//and starts playing mplayer.seekto (position) directly from the specified location;p osition = 0;} catch (Exception e) {e.printstacktrace ();}}} @Overridepublic void surfacedestroyed (Surfaceholder holder) {}}//When other activity is opened, pauses playback of @overrideprotected void OnPause ( {if (mplayer.isplaying ()) {//Saves the current playback position position = Mplayer.getcurrentposition (); Mplayer.stop ();} Super.onpause ();} @Overrideprotected void OnDestroy () {//Stop playing if (mplayer.isplaying ()) mplayer.stop ();//Release Resource mplayer.release (); Super.ondestroy ();}}
Android Play Video