java - Using MediaPlayer in a Service Class...? -


i'm making app, , i'm working on adding background music it. i've seen lot of ways that, , i've chosen make using service. problem, however, comes when call service, due hear nothing, music doesn't play! i've compared examples, , tried everything, there's no change...instead meanwhile i'm using intentservice play music (to have @ least something), isn't pretty good, cos example when press home button, music continues playing...

the code of service class following:

    package edu.ub.pis2016.darmas.entrega1;       import android.app.service;     import android.content.context;     import android.content.intent;     import android.media.audiomanager;     import android.media.mediaplayer;     import android.os.ibinder;     import android.widget.toast;       public class service_1 extends service {          mediaplayer player;           public ibinder onbind(intent arg0) {              return null;         }          @override         public void oncreate(){             super.oncreate();             player = mediaplayer.create(this, r.raw.calm);             player.setlooping(true); // set looping             player.setvolume(100,100);         }          @override         public int onstartcommand(intent intent, int flags, int startid){             player.start();              return 1;         }           @override         public void ondestroy(){             player.stop();             player.release();             player = null;         }           public void onstop(){             player.stop();         }          public void onpause(){          }       } 

and call service 1 of activities, doing:

intent intent = new intent(this, service_1.class); startservice(intent); 

but doesn't work... instead, here's code of intentservice class:

    package edu.ub.pis2016.darmas.entrega1;      import android.app.intentservice;     import android.content.context;     import android.content.intent;     import android.media.audiomanager;     import android.media.mediaplayer;     import android.os.ibinder;     import android.view.keyevent;     import android.widget.toast;      /*     *  service_music class extends intentservice class.      due latter,     *  there's no need implement thread creates  default (a worker thread),     *  runs in background. also, class implements  audiomanager, in order     *  control different situations in music can found  through phone.     *     */     public class service_music extends intentservice implements audiomanager.onaudiofocuschangelistener {          mediaplayer mp;         audiomanager audiomanager;         boolean end = false;            /*         *  ------------------------------------------------         *  -            class methods              -         *  ------------------------------------------------         */          // constructor class, required! create here our worker thread         public service_music() {             super("service_music_thread");         }           public ibinder onbind(intent arg0) {              return null;         }          @override         protected void onhandleintent(intent intent) {              lets_play_music();             while (end != true){}         }           @override         public void ondestroy(){             super.ondestroy(); // it's mandatory call super() methods when extending intentservice             if (this.mp != null) releasemediaplayer();          }           /*         *  ------------------------------------------------         *  -             initialize methods               -         *  ------------------------------------------------         */          // method initialize media player         private void initializemediaplayer(){             //this.mp = mediaplayer.create(this, r.raw.calm);             this.mp = mediaplayer.create(this, r.raw.barefootandbruisdjamestownstory);             this.mp.setlooping(true);             this.mp.start();         }           private void initializeaudiomanager(){             this.audiomanager = (audiomanager) getsystemservice(context.audio_service);             int result = audiomanager.requestaudiofocus(this, audiomanager.stream_music,             audiomanager.audiofocus_gain);              if (result != audiomanager.audiofocus_request_granted) {                 // not audio focus.                 toast.maketext(getapplicationcontext(), "ha habido un error al iniciar la musica :(", toast.length_short).show();             }         }          /*         *  ------------------------------------------------         *  -               auxiliar methods               -         *  ------------------------------------------------         */          // method carry task of playing music, notice local media!         private void lets_play_music(){             initializeaudiomanager();             initializemediaplayer();             //initializeaudiomanager();             //this.mp.start();         }           // method release resources taken mp object         private void releasemediaplayer(){             this.mp.stop();             this.mp.release();             this.mp = null;         }           /*         *  ------------------------------------------------         *  -            audio manager methods             -         *  ------------------------------------------------         */          public void onaudiofocuschange(int focuschange) {             // based on focus change...             switch (focuschange){                  // case if have gained focus play!                 case audiomanager.audiofocus_gain:                     if (this.mp == null) initializemediaplayer(); // if had release mp reason, re-initialize it!                     else if(!this.mp.isplaying()) {                        this.mp.setlooping(true);  // in theory, should play looping through infinitely                        this.mp.start(); // if it's not playing, starts!                     }                     this.mp.setvolume(1.0f, 1.0f);  // set volume!                     break;                  // case if have (or almost) lost focus                 case audiomanager.audiofocus_loss:                     if (this.mp.isplaying()) this.mp.stop(); // if it's playing, have stop first                     end = true;                     releasemediaplayer();  // release resources                     break;                 // case if have lost focus short period of time                 case audiomanager.audiofocus_loss_transient:                     if (this.mp.isplaying()) this.mp.pause(); // if it's playing, pause it!                     break;                  // case if have lost focus tiny piece of time, we're allowed continue performing music in quiet state!                 case audiomanager.audiofocus_loss_transient_can_duck:                     if (this.mp.isplaying()) this.mp.setvolume(0.3f, 0.3f); // if it's playing, set volume quiet level!                     break;             }         }       } 

this latter works, not 'audiomanager', , when press 'home button' music continues playing...

if me, or tell me error, or i'm doing bad, fix this, i'd extremely grateful, because i'm turning mad this...

two guesses (due lack of full code):

your manifest doesn't contain service, doesn't receive intent r.raw.value doesn't exist


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -