Android Development MediaPlayer and Surfaceview play video

Source: Internet
Author: User

Using MediaPlayer to play audio, you can also play video files, except when playing video with MediaPlayer, the image output interface is not available. You can use the Surfaceview component to display video images. Using MediaPlayer and Surfaceview to play a video can be divided into the following four steps:

1) define the Surfaceview component. It is best implemented in the layout file.

2) Create the MediaPlayer object and load the video for it to play.

3) Output The video playback to the Surfaceview. Using the Setdisplay method of the MediaPlayer object, you can output the video footage you are playing to Surfaceview.

4) Call the appropriate method of the MediaPlayer object to control the playback of the video. Play play, pause, pause, stop.

An example is as follows:


Import Java.io.ioexception;import Android.app.activity;import Android.media.mediaplayer;import Android.media.mediaplayer.oncompletionlistener;import Android.os.bundle;import Android.view.SurfaceView;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Activity {private MediaPlayer mp;//declaration MediaPlayer Object Private Surfaceview sv;//declares Surfaceview object @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (s        Avedinstancestate);        Setcontentview (R.layout.main); Mp=new MediaPlayer ();//Instantiate MediaPlayer object sv= (Surfaceview) Findviewbyid (R.ID.SURFACEVIEW1);// Gets the Surfaceview component that is added in the Layout Manager button play= (button) Findviewbyid (r.id.play);//Gets the Play button final button pause= (button) F Indviewbyid (r.id.pause);//get "Pause/Resume" button stop= (button) Findviewbyid (r.id.stop);//Get "stop" button//Add click event for "Play" button Listener Play.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {mp.reset ();//Reset MediaPlayer Object try {mp.setdatasource ("/sdcard/ccc.mp4");// Set the video Mp.setdisplay (Sv.getholder ()) to play,//Set the video output to Surfaceviewmp.prepare ();//Pre-load video Mp.start ();// Start playing Sv.setbackgroundresource (r.drawable.bg_playing);//Change the background image of Surfaceview pause.settext ("pause");p ause.setenabled ( true)///Set "Pause" button available} catch (IllegalArgumentException e) {e.printstacktrace ();} catch (SecurityException e) { E.printstacktrace ();}        catch (IllegalStateException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}); Add a click event listener for the Stop button Stop.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {if (mp.isplaying ()) {m P.stop ();//Stop playing Sv.setbackgroundresource (r.drawable.bg_finish);//change Surfaceview background image pause.setenabled (false);//Set        "Pause" button not available}}); Add a click event listener for the pause button Pause.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {//TODO Auto-genera Ted Method Stubif (Mp.isplaying ()) {mp.pause ();//Pause the playback of the video ((button) v). SetText ("continue");Else{mp.start ();//Continue video playback ((button) v). SetText ("pause");}}); Add complete event listener Mp.setoncompletionlistener for MediaPlayer object (new Oncompletionlistener () {public void oncompletion (Mediaplay ER MP) {//TODO auto-generated method Stubsv.setbackgroundresource (r.drawable.bg_finish);// Change the background image of Surfaceview Toast.maketext (Mainactivity.this, "video playback is complete! ", Toast.length_short). Show ();}});    @Overrideprotected void OnDestroy () {if (mp.isplaying ()) {mp.stop ();//Stop playing video}mp.release ();//Release Resources Super.ondestroy ();} }

All right, it's over.



Android Development MediaPlayer and Surfaceview play video

Related Article

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.