Code: One 1. Play music from the local file system Musicbutton.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub String sdcard=environment. getexternalstoragedirectory (). GetPath (); Try { Mediaplayer.setdatasource (sdcard+file. Separator+ "Music.mp3"); } 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 (); } Try { Mediaplayer.prepare (); } catch (IllegalStateException e) { TODO auto-generated Catch block E.printstacktrace (); } catch (IOException e) { TODO auto-generated Catch block E.printstacktrace (); } Mediaplayer.start (); Toast. Maketext (Tmediaplayer. This, "playing music in the local file system", Toast. length_short). Show (); } }); 2. Play music from the project source file using service services Public class MyService extends Service { Private MediaPlayer MP; @Override Public IBinder Onbind (Intent arg0) { TODO auto-generated Method stub return null; } @Override Public void onCreate () { Super. OnCreate (); Mp=mediaplayer. Create (this, R.raw. AA); Log. I ("Aaaaaaaaa", "Musicservice OnCreate is run"); } Public int Onstartcommand (Intent Intent, int flags, int startid) { TODO auto-generated Method stub Mp.start (); Log. I ("Gggggggggggg", "Musicservice Onstartcommand is run"); Toast. Maketext (MyService. This, "playing music from source file ~", Toast. length_short). Show (); return Super. Onstartcommand (Intent, flags, Startid); } 3. Play Video File file=new file ("/sdcard/movies/shudu.mp4"); Log. I ("VVVVVVVVVVVVV", "33333333333"); mccontroller=New Mediacontroller (Video. this); if (File.exists ()) { Videoview.setvideopath (File.getabsolutepath ()); Videoview.setmediacontroller (Mccontroller); Videoview.requestfocus (); Try { Videoview.start (); }catch(Exception e) { E.printstacktrace (); } Log. I ("Ffffffff", "33333333333"); Videoview.setoncompletionlistener (new Oncompletionlistener () { @Override Public void oncompletion (MediaPlayer arg0) { TODO auto-generated Method stub } }); 4. Stop Music playback Pausebtn.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub StopService (Intent); Mediaplayer.stop (); Toast. Maketext (Tmediaplayer. This, "Stop playing", Toast. length_short). Show (); } }); 5. Exit Exitbtn.setonclicklistener (new Onclicklistener () { @Override Public void OnClick (View arg0) { TODO auto-generated Method stub StopService (Intent); Intent.setclass (Tmediaplayer. this, loginactivity. class); StartActivity (Intent); } }); Run Result: () First, play local file music Second, play the source file music Third, stop playing music Iv. playing video |