multithreading - Android making thread in service -
i'm making mediaplayer service , don't want use application resource service. read must create new thread in service,but i'm beginner , can't understand how this. know how start new thread don't know how process startplay , stopall method in thread
package com.filsoft.mouse; import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.os.binder; import android.os.ibinder; public class sound extends service implements oncompletionlistener { mediaplayer[] mediaplayer=new mediaplayer[3]; private final ibinder mbinder = new localbinder(); @override public ibinder onbind(intent intent) { return mbinder; } @override public void oncreate() { mediaplayer[0] = mediaplayer.create(this, r.raw.sound1); mediaplayer[0].setoncompletionlistener(this); mediaplayer[0].setlooping(true); mediaplayer[1] = mediaplayer.create(this, r.raw.sound2); mediaplayer[1].setoncompletionlistener(this); mediaplayer[1].setlooping(true); mediaplayer[2] = mediaplayer.create(this, r.raw.sound3); mediaplayer[2].setoncompletionlistener(this); mediaplayer[2].setlooping(true); } public void startplay(int idx) { stopall(); if (!mediaplayer[idx].isplaying()) { mediaplayer[idx].start(); } } 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 stopall() { if (mediaplayer[0].isplaying()) { mediaplayer[0].stop(); } if (mediaplayer[1].isplaying()) { mediaplayer[1].stop(); } if (mediaplayer[2].isplaying()) { mediaplayer[2].stop(); } } public void ondestroy() { stopall(); mediaplayer[0].release(); mediaplayer[1].release(); mediaplayer[2].release(); } public void oncompletion(mediaplayer _mediaplayer) { stopself(); } }
Comments
Post a Comment