multithreading - Android MediaPlayer use lot of resource -


i'm making game in opengl 2.0 , have problems sounds, because sounds slow down application , fps decrease 20 frames. implement service sounds , run on new thread, problem same. mediaserver use more cpu application lot of sprites. play 3 sounds total size less 0.5 mb. code:

package com.filsoft.mouse;  import java.io.ioexception;  import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.os.binder; import android.os.handler; import android.os.handlerthread; import android.os.ibinder; import android.os.looper; import android.os.message; import android.os.process;  public class sound extends service implements oncompletionlistener {       private final ibinder mbinder = new localbinder();       private looper mservicelooper;       private servicehandler mservicehandler;     // handler receives messages thread       private final class servicehandler extends handler {           mediaplayer[] mediaplayer=new mediaplayer[3];           public servicehandler(looper looper) {               super(looper);             mediaplayer[0] = mediaplayer.create(getapplicationcontext(), r.raw.sound1);             mediaplayer[0].setlooping(true);              mediaplayer[1] = mediaplayer.create(getapplicationcontext(), r.raw.sound1);             mediaplayer[1].setlooping(true);              mediaplayer[2] = mediaplayer.create(getapplicationcontext(), r.raw.sound1);             mediaplayer[2].setlooping(true);                 try {                     for(int i=0;i<3;i++) mediaplayer[i].prepare();                 } catch (illegalstateexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             }           @override           public void handlemessage(message msg) {               play(msg.arg1);           }           public void stopall()           {                if (mediaplayer[0].isplaying()) {                       mediaplayer[0].pause();                     }                     if (mediaplayer[1].isplaying()) {                           mediaplayer[1].pause();                     }                     if (mediaplayer[2].isplaying()) {                           mediaplayer[2].pause();                     }           }           public void play(int idx)           {               stopall();               if (!mediaplayer[idx].isplaying()) {                   mediaplayer[idx].start();                 }           }        }        @override       public ibinder onbind(intent intent) {         return mbinder;       }        @override       public void oncreate() {           handlerthread thread = new handlerthread("audio",                     process.thread_priority_urgent_audio);             thread.start();              // handlerthread's looper , use our handler             mservicelooper = thread.getlooper();             mservicehandler = new servicehandler(mservicelooper);          }       public void startplay(int idx)       {           message msg = mservicehandler.obtainmessage();           msg.arg1 = idx;           mservicehandler.sendmessage(msg);        }       public class localbinder extends binder {             public sound getservice() {                 return sound.this;             }          }       @override       public int onstartcommand(intent intent, int flags, int startid) {           return start_sticky;       }        public void ondestroy() {        }        public void oncompletion(mediaplayer _mediaplayer) {         stopself();       }      } 

instead of creating multiple mediaplayers, should use soundpool - class designed play multiple streams simultaneously such found in games. looping sounds easy:

sounds can looped setting non-zero loop value. value of -1 causes sound loop forever. in case, application must explicitly call stop() function stop sound. other non-zero value cause sound repeat specified number of times, e.g. value of 3 causes sound play total of 4 times.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -