Code: protected void onCreate (Bundle savedinstancestate) { TODO auto-generated Method stub Super. OnCreate (savedinstancestate); Setcontentview (r.layout. Mediapaly); Btnstart= (ImageButton) Findviewbyid (r.id. Btnstart); Btnpause= (ImageButton) Findviewbyid (r.id. Btnpause); btnstop= (ImageButton) Findviewbyid (r.id. Btnstop); Btnyuan= (Button) Findviewbyid (r.id. Yuan); Netbtn= (Button) Findviewbyid (r.id. NET); Benbtn= (Button) Findviewbyid (r.id. Bendi); Tingbtn= (Button) Findviewbyid (r.id. Tingzhi); Tuibtn= (Button) Findviewbyid (r.id. Tui); tv= (TextView) Findviewbyid (r.id. TV); Btnstart.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub Player=mediaplayer. Create (Mediaplay. this, R.raw. nan); Player.start (); Tv.settext ("Playing music ....") "); } }); Btnstop.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub if (Player.isplaying ()) { Player.stop (); Tv.settext ("Music stops playing! "); } } }); Btnpause.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub if (Player.isplaying ()) { Player.pause (); Tv.settext ("Music Paused! "); }Else{ Player.start (); } } }); Btnyuan.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub Player=mediaplayer. Create (Mediaplay. this, R.raw. nan); Player.start (); } }); file=New File (environment. getExternalStorageDirectory(). GetPath () + "/nan.mp3"); if (File.exists ()) { Player=mediaplayer. Create (Mediaplay. this, Uri. Parse (File.getabsolutepath ())); }Else { Toast. Maketext (Mediaplay. This, "The file does not exist!" ", Toast. Length_long). Show (); } Benbtn.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub Player.reset (); Try { Player.setdatasource (File.getabsolutepath ()); Player.prepare (); Player.start (); } catch (IllegalArgumentException e) { TODO auto-generated Catch block E.printstacktrace (); } catch (SecurityException 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 (); } } }); Tingbtn.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub if (Player.isplaying ()) { Player.pause (); Tv.settext ("Music Paused! "); }Else{ Player.start (); } } }); Tuibtn.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub if (Player.isplaying ()) { Player.stop (); Tv.settext ("Music stops playing! "); } } }); } Run: |