package dellwon.ch8.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; public class PlayService extends Service implements Runnable{ MediaPlayer player; boolean flag=true; private int duration; private int cuttentPosition; // Bind ½ÃŲ ¾×ƼºñÀÌ¿¡¼­ µ¥ÀÌÅÍ È¹µæÇϱâ À§ÇØ È£Ãâ ÇÒ ¸ñÀû public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public int getCuttentPosition() { return cuttentPosition; } public void setCuttentPosition(int cuttentPosition) { this.cuttentPosition = cuttentPosition; } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub player=MediaPlayer.create(this, R.raw.kara); setDuration(player.getDuration()); player.start(); //MediaPlay ÀÇ À½¿ø Ç÷¹ÀÌ ½Ã°£À» ¾ò¾î³»¼­ serviceÀÇ µ¥ÀÌÅ͸¦ //¾÷µ¥ÀÌÆ® ½ÃÅ°´Â ÀÛ¾÷À» ÇÏ´Â thread Thread t=new Thread(null, this, "player"); t.start(); return new MyBinder(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); flag=false; player.stop(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); // player=MediaPlayer.create(this, R.raw.kara); // player.start(); } // Activity bind ½Ã Activity¿¡ Àü´Þ ½Ãų °´Ã¼ public class MyBinder extends Binder{ PlayService getService(){ return PlayService.this; } } //thread ¿¡ ÀÇÇØ ½ÇÇØ µÇ´Â ÄÚµå public void run() { // TODO Auto-generated method stub while(flag){ setCuttentPosition(player.getCurrentPosition()); try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } } } }