MediaPlayer can play the video in addition to audio playback, but it is not enough to rely on MediaPlayer to play the video. There is also a need to write a space that can be used for video display, which requires the ability to update the GUI at a high speed, and to render the GUI in a non-clogging way while rendering the code, assuming this function is complete, You must rely on the Android.view.SurfaceView component to complete. The Surfaceview component encapsulates a surface object rather than a canvas object, and using surface to complete control of the background thread can be very useful in places such as video, 3D graphics, which require high-speed updates or high frame rates.
Surfaceholder displays an abstract interface to your surface, allowing you to control the size and formatting of your surface. and edit pixels on your surface, and monitor surace changes. This interface is typically implemented through the Surfaceview class.
The following is a sample written in the previous section to achieve MediaPlayer video playback.
the Surfaceview object is first configured in XML.
<span style= "FONT-SIZE:18PX;" ><relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <imagebutton android:id=" @+id/imagebutton1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop = "true" android:src= "@drawable/play"/><surfaceview android:id= "@+id/surfaceview" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" Android:layout_alignP Arentleft= "true" Android:layout_alignparentright= "true" android:layout_below= "@+id/imagebutton1"/><imagebutton android:id= "@+id/imagebutton2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_above = "@+id/surfaceview" android:layout_alignright= "@+id/surfaceview" android:src= "@drawable/stop"/><ImageView Android:id= "@+id/imageview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:lay Out_alignbottom= "@+id/surfaceview" android:layout_centerhorizontal= "true" android:layout_marginbottom= "162DP"/&G T;</relativelayout></span>
java files
<span style= "FONT-SIZE:18PX;" >package Com.example.mediaplayer2;import Java.io.file;import Android.app.activity;import Android.media.audiomanager;import Android.media.mediaplayer;import Android.os.bundle;import Android.os.environment;import Android.view.motionevent;import Android.view.surfaceholder;import Android.view.surfaceview;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onhoverlistener;import Android.widget.imagebutton;import Android.widget.imageview;public Class Mainactivity extends Activity {private ImageButton play = null;//Play private ImageButton stop = null;//stop playing private MEDIAPL Ayer media = null;private ImageView image=null;p rivate surfaceview surfaceview = null;private Surfaceholder SurfaceHolder = Null;private Boolean pauseflag=false; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Super.setcontentview (r.layout.activity_main); this.play = (ImageButton) Super.findviewbyid ( R.id.imageButton1); this.stop = (ImageButton) Super.findviewbyid (r.id.imagebutton2); This.surfaceview = (Surfaceview) Super.findviewbyid (R.id.surfaceview);//Instantiation Surfaceviewthis.surfaceholder = This.surfaceView.getHolder ();// Instantiate SurfaceHolderthis.surfaceHolder.setType (surfaceholder.surface_type_push_buffers);//Set Format this.image= ( ImageView) This.findviewbyid (r.id.imageview1); this.media = new MediaPlayer ();//Instantiate Meidaplayer component try {// Get the video file on the SD card string path = Environment.getexternalstoragedirectory () + File.separator + "Movies" + File.separator + " Abc.mp4 ";//Set Resource This.media.setDataSource (path); Some inferences are missing} catch (Exception e) {e.printstacktrace ();} Listen for This.play.setOnClickListener (New Playonclicklistenerimpl ()) to start the playback box stop playback, respectively, This.stop.setOnClickListener (new Stoponclicklistenerimpl ()); This.surfaceView.setOnClickListener (new Pauseorstart ()); This.surfaceView.setOnHoverListener (New Onhoverlistener () {public boolean onhover (View V, motionevent event) {//TODO Auto-generated Method Stubif (MainActivity.this.mediA.isplaying ()) {MainActivity.this.image.setImageResource (r.drawable.stop);} else {MainActivity.this.image.setImageResource (r.drawable.play);} return true;});} Private class Playonclicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) {// Set the audio format Ah MainActivity.this.media.setAudioStreamType (audiomanager.stream_music);// Set the playback range MainActivity.this.media.setDisplay (MainActivity.this.surfaceHolder); try {// Prepare to play MainActivity.this.media.prepare ();} catch (Exception e) {}//starts playing MainActivity.this.media.start ();}} Private class Stoponclicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) {//Stop playing if (Mainac Tivity.this.media!=null) {MainActivity.this.media.stop ();}}} Private class Pauseorstart implements onclicklistener{@Overridepublic void OnClick (View v) {//TODO auto-generated method STUBIF (mainactivity.this.media!=null) {if (MainActivity.this.pauseFlag) {MainActivity.this.media.start (); Mainactivity.this.pauseflag=false;} else {MainActivity.this.meDia.pause (); Mainactivity.this.pauseflag=true;}}}} </span>
This enables a simple video playback function, but because of the decoder now only support 3GP and some MP4 video playback, want to play many other video formats need to write an algorithm or download decoder implementation
Next forecast: audiomanagerandroid sound editor
Learn Android<mediaplayer from scratch with your own player (video playback). 49 .>