Android multimedia learning 6: using services to play background music

Source: Internet
Author: User

Android allows us to use service components to complete background tasks. The permission of these tasks does not affect other user interactions.

 

1. Activity Type

Package demo. camera; <br/> Import android. app. activity; <br/> Import android. content. componentname; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. serviceconnection; <br/> Import android. OS. bundle; <br/> Import android. OS. ibinder; <br/> Import android. view. view; <br/>/** <br/> * demonstrate how the activity uses the service to play the background audio. <br/> * bind the service and activity together. <br/> * @ author administrator <br/> */<br/> public class backgroundaudiodemo extends activity {</P> <p> private audioservice; </P> <p> // use serviceconnection to monitor service status changes <br/> private serviceconnection conn = new serviceconnection () {</P> <p> @ override <br/> Public void onservicedisconnected (componentname name) {<br/> // todo auto-generated method stub <br/> audioservice = NULL; <br/>}</P> <p> @ override <br/> Public void onserviceconnected (componentname, ibinder binder) {<br/> // here We instantiate audioservice, implement <br/> audioservice = (audioservice. audiobinder) binder ). getservice (); </P> <p >}< br/>}; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. back_audio); <br/>}</P> <p> Public void onclick (view v) {<br/> int id = v. GETID (); <br/> intent = new intent (); <br/> intent. setclass (this, audioservice. class); <br/> If (ID = R. id. btn_start) {<br/> // start the service and bind it to destroy the activity at the same time, check whether the song is still playing <br/> startservice (intent); <br/> bindservice (intent, Conn, context. bind_auto_create); <br/> finish (); <br/>} else if (ID = R. id. btn_end) {<br/> // end service <br/> unbindservice (conn); <br/> stopservice (intent); <br/> finish (); <br/>} else if (ID = R. id. btn_fun) {<br/> audioservice. havefun (); <br/>}< br/>

2. service type

Package demo. camera; <br/> Import android. app. service; <br/> Import android. content. intent; <br/> Import android. media. mediaplayer; <br/> Import android. OS. binder; <br/> Import android. OS. ibinder; <br/> Import android. widget. mediacontroller. mediaplayercontrol; <br/>/** <br/> * to enable playing music in the background, the service <br/> * service is used to complete some actions that do not need to interact with users in the background. <br/> * @ author administrator <br/> * /<br/> public class Audioservice extends Service implements mediaplayer. oncompletionlistener {</P> <p> mediaplayer player; </P> <p> private final ibinder binder = new audiobinder (); <br/> @ override <br/> Public ibinder onbind (intent arg0) {<br/> // todo auto-generated method stub <br/> return binder; <br/>}< br/>/** <br/> * this action is triggered when audio is played <br/> */<br/> @ override <br /> Public void oncompletion (mediaplayer player) {<br/> // t Odo auto-generated method stub <br/> stopself (); // it's over, end Service <br/>}</P> <p> // here we need to instantiate the mediaplayer object <br/> Public void oncreate () {<br/> super. oncreate (); <br/> // obtain an MP3 file from the raw folder. <br/> player = mediaplayer. create (this, R. raw. TT); <br/> player. setoncompletionlistener (this); <br/>}</P> <p>/** <br/> * This method is available only in sdk2.0, replace the original onstart method <br/> */<br/> Public int onstartcommand (intent, int flags, Int startid) {<br/> If (! Player. isplaying () {<br/> player. start (); <br/>}< br/> return start_sticky; <br/>}</P> <p> Public void ondestroy () {<br/> // super. ondestroy (); <br/> If (player. isplaying () {<br/> player. stop (); <br/>}< br/> player. release (); <br/>}</P> <p> // to interact with the activity, we need to define a binder object <br/> class audiobinder extends binder {</P> <p> // return the service object <br/> audioservice getservice () {<br/> return audioservice. this; <br/>}</P> <p> // playback progress <br/> Public void havefun () {<br/> If (player. isplaying () & player. getcurrentposition ()> 2500) {<br/> player. seekto (player. getcurrentposition ()-2500); <br/>}< br/>

3. Configure the service in the configuration file androidmanifest. xml.

 

<Service
Android: Name = ". audioservice"/>

 

 

Related Article

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.