MediaPlayer for Android

Source: Internet
Author: User

I have written 14 introductory articles on the interface. After reading and following the exercises, I have a certain understanding of the commonly used Layout and View, the next article will not emphasize the introduction interface, but will focus on the specific common functions. This article describes how to use MediaPlayer. MediaPlayer can play audio and video, and VideoView can also be used to play video. Although VideoView is easier to use than MediaPlayer, it is better to use MediaPlayer for customization, depending on the situation. MediaPlayer is relatively simple to play audio, but SurfaceView is required to play the video. SurfaceView has more advantages in drawing than a general custom View. It supports a full OpenGL ES library. First, paste the program running result of this article. The above is the playing/stopping audio, and the progress can be adjusted using SeekBar. below is the playing/stopping video, and the progress is also adjusted using SeekBar: main. xml source code: [xhtml] view plaincopyprint? <? 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 = "wr Ap_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 = "video playback"> </Button> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Stop playing" android: id = "@ + id/Button04"> </Button> </LinearLayout> <? 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_conte Nt "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" andro Id: text = "video playback"> </Button> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Stop playing" android: id = "@ + id/Button04"> </Button> </LinearLayout> the source code of the program in this article is a bit long: [java] view plaincopyprint? 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; public class testMedia extends Activity {/** Called when the activity is first 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; // mutex variable, prevent degree conflict when the timer and SeekBar are dragged @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // ---------- Media control settings --------- // m = new MediaPlayer (); // a prompt is displayed after the playback ends. setOnCompletionListener (new MediaPlayer. onCompletionListener () {@ Override public void onCompletion (MediaPlayer arg0) {Toast. makeText (testMedia. this, "end", 1000 ). show (); m. release () ;}}); // ---------- the Timer records the playback 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 processing */class ClickEvent implements View. onClickListener {@ Override public void onClick (View v) {if (v = btn_start_audio) {m. reset (); // restore to uninitialized status m = MediaPlayer. create (testMedia. this, R. raw. big); // read the audio skb_audio.setMax (m. getDuration (); // set the SeekBar length try {m. prepare (); // prepare} catch (IllegalStateException 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 (); // restore to uninitialized status m = MediaPlayer. create (testMedia. this, R. raw. test); // read the video skb_video.setMax (m. getDuration (); // set the length of 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 ;}}}

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.