Code: 1.MainActivity Package com.example.mediapiayer; Import Java.io.File; Import java.io.IOException; Import Android.media.MediaPlayer; Import Android.media.MediaPlayer.OnCompletionListener; Import Android.net.Uri; Import Android.os.Bundle; Import android.os.Environment; Import android.app.Activity; Import android.content.Intent; Import Android.view.Menu; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.TextView; public class Mainactivity extends Activity { Public MediaPlayer myPlayer1 = new MediaPlayer (); Private Button bt1, BT2, BT3, Bt4, Bt5, BT6; Private TextView MTextView1; Private String uri = "http://bd.kuwo.cn/yinyue/2324240"; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); MTextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1); BT1 = (Button) Findviewbyid (R.id.button1); BT2 = (Button) Findviewbyid (R.id.button2); BT3 = (Button) Findviewbyid (R.id.button3); BT4 = (Button) Findviewbyid (R.ID.BUTTON4); BT5 = (Button) Findviewbyid (R.ID.BUTTON5); BT6 = (Button) Findviewbyid (R.ID.BUTTON6); Bt1.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View arg0) { TODO auto-generated Method Stub try { if (myplayer1.isplaying () = = True) { Myplayer1.reset (); } MyPlayer1 = Mediaplayer.create (Mainactivity.this, R.raw.song); Myplayer1.start (); Mtextview1.settext (R.string.str_start); } catch (IllegalStateException e) { Mtextview1.settext (R.string.str_stop); E.printstacktrace (); } } }); Bt2.setonclicklistener (New Onclicklistener () { String sdcard = Environment.getexternalstoragedirectory (). GetPath (); @Override public void OnClick (View arg0) { TODO auto-generated Method Stub try { if (myplayer1.isplaying () = = True) { Myplayer1.reset (); } Myplayer1.setdatasource (SDcard + file.separator + "F.I.R.-Yueyawan.mp3"); Myplayer1.prepare (); Myplayer1.start (); Mtextview1.settext (R.string.str_tishi); } catch (IllegalStateException e) { Mtextview1.settext (R.string.str_stop); E.printstacktrace (); } catch (IllegalArgumentException e) { TODO auto-generated Catch block E.printstacktrace (); } catch (SecurityException e) { TODO auto-generated Catch block E.printstacktrace (); } catch (IOException e) { TODO auto-generated Catch block E.printstacktrace (); } } }); Bt3.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View arg0) { TODO auto-generated Method Stub Myplayer1.reset (); try { Myplayer1.setdatasource (URI); } catch (IllegalArgumentException e) { TODO auto-generated Catch block E.printstacktrace (); } catch (SecurityException e) { TODO auto-generated Catch block E.printstacktrace (); } catch (IOException e) { TODO auto-generated Catch block E.printstacktrace (); } Intent Intent = new Intent (Intent.action_view, Uri.parse (Uri)); StartActivity (Intent); Mtextview1.settext (R.string.str_wangluo); } }); Bt4.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View arg0) { TODO auto-generated Method Stub Myplayer1.stop (); Mtextview1.settext (r.string.str_finished); } }); Bt5.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View arg0) { TODO auto-generated Method Stub } }); Bt6.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View arg0) { TODO auto-generated Method Stub Myplayer1.stop (); Mtextview1.settext (R.string.str_jieshu); } }); } @Override public boolean Oncreateoptionsmenu (Menu menu) { Inflate the menu; This adds items to the action bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; } } 2.activity_main.xml <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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:background="@drawable/u311593540665946679fm15gp0" Tools:context=". Mainactivity " > <textview android:id="@+id/textview1" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_alignparenttop="true" Android:layout_centerhorizontal="true" Android:text = "Test multimedia playback" /> <button android:id="@+id/button1" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_alignleft="@+id/button2" android:layout_alignright="@+id/button2" android:layout_below="@+id/textview1" android:layout_margintop="26DP" Android:text = "Play music from the source file" /> <button android:id="@+id/button3" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_alignleft="@+id/button2" android:layout_alignright="@+id/button2" android:layout_below="@+id/button2" Android:text = "Play Music on the network" /> <button android:id="@+id/button2" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_below="@+id/button1" Android:layout_centerhorizontal="true" Android:text = "Play music in the local file system" /> <button android:id="@+id/button4" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_alignbaseline="@+id/button5" android:layout_alignbottom="@+id/button5" android:layout_alignleft="@+id/button3" Android:text = "Stop playing" /> <button android:id="@+id/button5" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_below="@+id/button3" android:layout_torightof="@+id/button4" Android:text = "Play video" /> <button android:id="@+id/button6" Android:layout_width="Wrap_content" android:layout_height="Wrap_content" android:layout_below="@+id/button3" android:layout_torightof="@+id/button5" Android:text = "Exit" /> </RelativeLayout> Run Result: () 1. 2. |