Learn Android<mediaplayer from scratch with the player (video playback). 49 .>

Source: Internet
Author: User

MediaPlayer In addition to audio playback, but also can play the video, but if you want to play video only rely on MediaPlayer is not enough, you also need to write a video display space, and this display space requirements can be quickly updated GUI The GUI can be rendered without blocking while rendering the code, and if you want to do this, You must rely on the Android.view.SurfaceView component to complete, and the Surfaceview component encapsulates a surface object instead of a canvas object, using surface to control the background thread, for videos, 3D graphics and so on need to quickly update or high frame rate where there is a great use.

Surfaceholder displays an abstract interface to your surface, allowing you to control the size and formatting of your surface, edit pixels on your surface, and monitor surace changes. This interface is typically implemented through the Surfaceview class.


The following is a combination of examples 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); A few judgments are missing} catch (Exception e) {e.printstacktrace ();} Listen 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 the decoder now only supports 3GP and some MP4 video playback, want to play more video format need to write algorithm or download decoder implementation

Next forecast: audiomanagerandroid sound editor

Learn Android<mediaplayer from scratch with the player (video playback). 49 .>

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.