Android improves MediaPlayer audio and video playback _android

Source: Internet
Author: User
Tags prepare

The previous article has introduced the Android interface of the introduction of the technology, I believe that everyone after reading and follow the practice, the common layout and view will have a certain understanding, and then no longer emphasize the introduction of the interface, but for the specific common features to unfold.

This article will introduce the use of MediaPlayer. MediaPlayer can play audio and video, and can also play video through Videoview, although videoview is easier to use than MediaPlayer, but customization is not as good as MediaPlayer, which needs to be chosen as appropriate. 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 source code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <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" ></SeekBar> <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= "Wrap_content" android:text= "Play audio" ></Button> < Button android:id= "@+id/button02" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android : text= "Stop playing" ></Button> </LinearLayout> <seekbar android:id= "@+id/seekbar02" Android:layout_ height= "Wrap_content" android:layout_width= "fill_parent" ></SeekBar> <surfAceview android:id= "@+id/surfaceview01" android:layout_width= "fill_parent" android:layout_height= "250px" ></ surfaceview> <linearlayout android:id= "@+id/linearlayout02" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" > <button android:layout_width= "wrap_content" android:layout_height= "WRAP_" Content "android:id=" @+id/button03 "android:text=" Play video "></Button> <button android:layout_width=" Wrap_
 Content "android:layout_height=" wrap_content "android:text=" Stop playing android:id= "@+id/button04" ></Button>

 </LinearLayout> </LinearLayout>

Java part of the source code a bit long, we need to be patient to read, detailed procedures are as follows:

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_audio=
  Null 
  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;//mutually exclusive variable to prevent the timer and Seekbar drag when the Progress conflict @Override PubLIC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    
    Setcontentview (R.layout.main);
    
    ----------Media Control Settings---------//M=new MediaPlayer (); Pop-up prompts after playback M.setoncompletionlistener (new Mediaplayer.oncompletionlistener () {@Override public void oncompletion (
  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 try {m.prepare ();//Prepare} catch (I  
  Llegalstateexception e) {//TODO auto-generated catch block E.printstacktrace ();  
  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 the length of the Seekbar 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 public void
 Onprogresschanged (SeekBar SeekBar, int progress, Boolean Fromuser) {//TODO auto-generated method stub} @Override
 public void Onstarttrackingtouch (SeekBar SeekBar) {ischanging=true;
   @Override public void Onstoptrackingtouch (SeekBar SeekBar) {M.seekto (seekbar.getprogress ()); 
 Ischanging=false;

 }
 }
}

I hope this article on the use of MediaPlayer can be helpful to everyone.
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.