21 days to learn the mediaplayer_android of Android development Tutorials

Source: Internet
Author: User
Tags prepare

This article describes the use of MediaPlayer. MediaPlayer can play audio and video, in addition, can also be played through the Videoview video, although videoview than MediaPlayer easy to use, but the customization is not as good as the MediaPlayer, depending on the situation selected. MediaPlayer audio is easier to play, but Surfaceview is needed to play the video. Surfaceview has the advantage of drawing more than ordinary custom view, which supports the full OpenGL ES Library.
First paste the results of the operation of this program screenshot, above is the play/Stop audio, available Seekbar to adjust the progress, the following is the play/stop video, but also with seekbar to adjust the progress:

Main.xml's source code:

<linearlayout android:id= "@+id/linearlayout01" android:layout_width= fill_parent "android:layout_height=" Fill_ Parent "xmlns:android=" http://schemas.android.com/apk/res/android "android:orientation=" vertical "> <seekbar Android:id= "@+id/seekbar01" android:layout_height= "wrap_content" android:layout_width= "Fill_parent" > < LinearLayout android:id= "@+id/linearlayout02" android:layout_width= wrap_content "android:layout_height=" Wrap_ Content "> <button android:id=" @+id/button01 "android:layout_width=" Wrap_content "android:layout_height=" W 
      Rap_content "android:text=" Play audio > <button android:id= "@+id/button02" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Stop playing" > <seekbar android:id= "@+id/seekbar02" Android: layout_height= "Wrap_content" android:layout_width= "fill_parent" > <surfaceview android:id= "@+id/SurfaceView0" 1 "android:layout_width=" Fill_parent "Android:layout_height= "250px" > <linearlayout android:id= "@+id/linearlayout02" android:layout_width= "Wrap_content" Andro id:layout_height= "Wrap_content" > <button android:layout_width= "wrap_content" android:layout_height= "WRAP_" 
      Content "android:id=" @+id/button03 "android:text=" Play Video "> <button android:layout_width=" wrap_content "
 android:layout_height= "Wrap_content" android:text= "Stop playing" android:id= "@+id/button04" >

This program's source code, a bit long:

Package Com.testmedia; 
Import java.io.IOException;
Import Java.util.Timer;
Import Java.util.TimerTask; 
Import android.app.Activity;
Import Android.media.AudioManager;
Import Android.media.MediaPlayer; 
Import Android.os.Bundle;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView; 
Import Android.view.View; 
Import Android.widget.Button;
Import Android.widget.SeekBar; 

Import Android.widget.Toast; The public class Testmedia extends activity {/** called the ' when ' is the ' The activity ' is a-created. Private SeekBar Skb_aud
 Private Button Btn_start_audio = null;

 Private Button Btn_stop_audio = null;
 Private SeekBar Skb_video=null; 
 Private Button btn_start_video = null;
 Private Button btn_stop_video = null; 
 Private Surfaceview Surfaceview; 
 Private Surfaceholder Surfaceholder; 
 Private MediaPlayer m = null;
 Private Timer Mtimer;
 Private TimerTask Mtimertask; Private Boolean ischanging=false;//mutex to prevent the timer from Seekbar when dragging a progress conflict @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.main);
  ----------Media Control Settings---------//M=new MediaPlayer (); Pop-up prompts M.setoncompletionlistener (new Mediaplayer.oncompletionlistener () {@Override public void Oncompl after playback finishes
        Etion (MediaPlayer arg0) {toast.maketext (Testmedia.this, "End", 1000). Show ();
      M.release ();
  ----------Timer record Play progress---------//Mtimer = new timer ();
     Mtimertask = new TimerTask () {@Override public void run () {if (ischanging==true) return;
     if (M.getvideoheight () ==0) skb_audio.setprogress (M.getcurrentposition ());
   Else Skb_video.setprogress (M.getcurrentposition ());

  Mtimer.schedule (mtimertask, 0, 10); 
  Btn_start_audio = (Button) This.findviewbyid (R.ID.BUTTON01); 
  Btn_stop_audio = (Button) This.findviewbyid (R.ID.BUTTON02);
  Btn_start_audio.setonclicklistener (New Clickevent ()); Btn_stop_audiO.setonclicklistener (New Clickevent ());
  skb_audio= (SeekBar) This.findviewbyid (R.ID.SEEKBAR01);
  Skb_audio.setonseekbarchangelistener (New Seekbarchangeevent ()); 
  Btn_start_video = (Button) This.findviewbyid (R.ID.BUTTON03); 
  Btn_stop_video = (Button) This.findviewbyid (r.id.button04);
  Btn_start_video.setonclicklistener (New Clickevent ());
  Btn_stop_video.setonclicklistener (New Clickevent ());
  skb_video= (SeekBar) This.findviewbyid (R.ID.SEEKBAR02);
  Skb_video.setonseekbarchangelistener (New Seekbarchangeevent ());
  Surfaceview = (Surfaceview) Findviewbyid (R.ID.SURFACEVIEW01);
  Surfaceholder = Surfaceview.getholder ();
  Surfaceholder.setfixedsize (100, 100);
 Surfaceholder.settype (surfaceholder.surface_type_push_buffers);
    }/* Key event Handling/* Class Clickevent implements view.onclicklistener{@Override public void OnClick (View v) {
      if (V==btn_start_audio) {m.reset ();//revert to an uninitialized state m=mediaplayer.create (Testmedia.this, r.raw.big);//Read audio Skb_audio.setMax (M.getduration ());//Set Seekbar length of try {m.prepare (); Preparing to catch (IllegalStateException e) {//TODO auto-generated catch block E.printsta        
      Cktrace ();        
      catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();  } m.start ();
    Play} else if (V==btn_stop_audio | | v==btn_stop_video) {m.stop (); else if (v==btn_start_video) {m.reset ();//revert to an uninitialized state m=mediaplayer.create (Testmedia.this, R.raw.test
      )//Read video Skb_video.setmax (M.getduration ());//Set Seekbar length M.setaudiostreamtype (audiomanager.stream_music);
      M.setdisplay (Surfaceholder);/set screen try {m.prepare ();
      catch (IllegalArgumentException e) {//TODO auto-generated catch block E.printstacktrace ();
   catch (IllegalStateException e) {//TODO auto-generated catch block     E.printstacktrace ();
      catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
    } m.start (); }}/* * SeekBar Progress Change Event/class Seekbarchangeevent implements seekbar.onseekbarchangelistener{@Override p ublic void onprogresschanged (SeekBar SeekBar, int progress, Boolean Fromuser) {//TODO auto-generated Method St
  UB} @Override public void Onstarttrackingtouch (SeekBar SeekBar) {ischanging=true;
   @Override public void Onstoptrackingtouch (SeekBar SeekBar) {M.seekto (seekbar.getprogress ());  


The above is 21 days to learn the Android development tutorial on the first article on the MediaPlayer of the relevant introduction, I hope to help you learn.

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.