Independently Developed music player 2

Source: Internet
Author: User
Package codefans. app. service; import Java. io. file; import Java. io. ioexception; import android. app. service; import android. content. intent; import android. media. mediaplayer; import android. media. mediaplayer. onpreparedlistener; import android. OS. binder; import android. OS. environment; import android. OS. ibinder; public class mp3service extends Service {private ibinder mybinder = new mp3ibinder (); Private mediaplaye R mediaplayer; private file musicfile; @ overridepublic void oncreate () {If (this. mediaplayer = NULL) {This. mediaplayer = new mediaplayer () ;}@ overridepublic void ondestroy () {If (this. mediaplayer! = NULL) {This. mediaplayer. stop (); this. mediaplayer. release (); this. mediaplayer = NULL;} super. ondestroy () ;}@ overridepublic ibinder onbind (intent) {return mybinder;} // start playing private void playmusci (string filename) {try {This. musicfile = new file (environment. getexternalstoragedirectory (), filename); this. mediaplayer. setdatasource (musicfile. getabsolutepath (); this. mediaplayer. prepare (); this. mediaplayer. setonpreparedlistener (New mypreparedlistenner ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalstateexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}// mediaplayer: The execution event is cached. Private class mypreparedlistenner implements onpreparedlistener {public void onprepared (mediaplayer MP) {mp3service. this. mediaplayer. start ();}} /*** this binder is returned when a new internal class activity calls Service * @ author codefans */private class mp3ibinder extends binder implements player {public void playmusic (string filepath) {mp3service. this. playmusci (filepath );}}}

Service playback

Package codefans. app. activity; import codefans. app. service. player; import codefans.mp3 layer. r; import android. app. activity; import android. content. componentname; import android. content. context; import android. content. intent; import android. content. serviceconnection; import android. OS. bundle; import android. OS. ibinder; import android. view. view; import android. widget. imagebutton; public class mp3playeractivity extends activity {private Boolean flag = false; private imagebutton playbt; private player binder; private serviceconnection myserviceconnection = new myserviceconnection (); /** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); this. playbt = (imagebutton) findviewbyid (R. id. play); intent = new intent ("codefans. action. mp3player "); bindservice (intent, myserviceconnection, context. bind_auto_create);} // play button click public void mediaplay (view v) {If (flag = false) // change the play button icon {playbt. setimageresource (R. drawable. stop); flag = true; this. binder. playmusic ("qqaudio");} else {playbt. setimageresource (R. drawable. play); flag = false; }}@ overrideprotected void ondestroy () {unbindservice (myserviceconnection); super. ondestroy ();} // The class private class myserviceconnection implements serviceconnection {public void onserviceconnected (componentname, ibinder Service) {mp3playeractivity is required to start the service. this. binder = (player) service;} public void onservicedisconnected (componentname name) {mp3playeractivity. this. binder = NULL ;}}}

It seems that the bindservice cannot be used here, because if the service of the activity is suspended, startservice should be used! It will be too late tomorrow (hey)

Only one playback function is provided.

Previous click to open the link

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.