Activity与Service之间交互并播放歌曲的实现代码

时间:2021-09-27 23:48:18

activity与service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进
 

Activity与Service之间交互并播放歌曲的实现代码

mservice:

复制代码 代码如下:

package com.tiantian.test;
import android.app.service;
import android.content.intent;
import android.media.mediaplayer;
import android.os.binder;
import android.os.environment;
import android.os.ibinder;
import android.util.log;
public class mservice extends service{
    mybinder mybinder = new mybinder();
    private mediaplayer mediaplayer;

    public class mybinder extends binder{
        mservice getservice(){
            return mservice.this;
        }
    }
    @override
    public ibinder onbind(intent intent) {
        // todo auto-generated method stub
        log.v("cat", "onbind");
        return mybinder;
    }
    @override
    public void oncreate() {
        // todo auto-generated method stub
        super.oncreate();
        log.v("cat", "oncreate");
        try {
            mediaplayer = new mediaplayer();
            mediaplayer.setdatasource(environment.getexternalstoragedirectory() + "/mp3/trhxn.mp3");
            mediaplayer.prepare();
        } catch (exception e) {
            // todo auto-generated catch block
            log.v("cat", "fail");
            e.printstacktrace();
        }
    }
    @override
    public void ondestroy() {
        // todo auto-generated method stub
        super.ondestroy();
        log.v("cat", "ondestroy");
    }
    @override
    public int onstartcommand(intent intent, int flags, int startid) {
        // todo auto-generated method stub
        log.v("cat", "onstartcommand");
        return super.onstartcommand(intent, flags, startid);
    }
    @override
    public boolean onunbind(intent intent) {
        // todo auto-generated method stub
        log.v("cat", "onunbind");
        return false;
    }

    public void start(){
        mediaplayer.start();
    }
    public void pause(){
        mediaplayer.pause();
    }
    public void stop(){
        mediaplayer.stop();
        mediaplayer.release();
    }
    public int getduration(){
        return mediaplayer.getduration();
    }
    public int getcurrentposition(){
        return mediaplayer.getcurrentposition();
    }
    public void seekto(int position){
        mediaplayer.seekto(position);
    }
}


musicplayactivity:

复制代码 代码如下:

package com.tiantian.test;
import android.app.activity;
import android.content.componentname;
import android.content.intent;
import android.content.serviceconnection;
import android.os.bundle;
import android.os.handler;
import android.os.ibinder;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.seekbar;
public class musicplayactivity extends activity {
    /** called when the activity is first created. */
    mservice mservice;
    private serviceconnection conn = new serviceconnection(){
        @override
        public void onserviceconnected(componentname arg0, ibinder arg1) {
            // todo auto-generated method stub
            mservice = ((mservice.mybinder)arg1).getservice();
            log.v("cat", "getserviced");
        }
        @override
        public void onservicedisconnected(componentname name) {
            // todo auto-generated method stub
            mservice = null;
        }

    };

    private seekbar seekbar;
    private button playbt;

    private boolean isplaying = false;
    private boolean isbinded = false;

    private handler mhandler;
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.main);
        intent intent = new intent(musicplayactivity.this, mservice.class);
        if(!isbinded){
            bindservice(intent, conn, bind_auto_create);
            isbinded = true;
        }
        seekbar = (seekbar) findviewbyid(r.id.seekbar);
        playbt = (button) findviewbyid(r.id.startbt);
        mhandler = new handler();
        seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() {

            @override
            public void onstoptrackingtouch(seekbar seekbar) {}

            @override
            public void onstarttrackingtouch(seekbar seekbar) {}

            @override
            public void onprogresschanged(seekbar seekbar, int progress,
                    boolean fromuser) {
                if(fromuser){
                    mservice.seekto((progress*mservice.getduration())/100);
                }
            }
        });
        playbt.setonclicklistener(new onclicklistener() {

            @override
            public void onclick(view v) {
                // todo auto-generated method stub
                if(!isplaying){
                    mservice.start();
                    isplaying = true;
                    playbt.settext("暂停");
                    mhandler.post(seekbarthread);
                }else{
                    mservice.pause();
                    isplaying = false;
                    playbt.settext("播放");
                    mhandler.removecallbacks(seekbarthread);
                }
            }
        });

    }

    runnable seekbarthread = new runnable() {

        @override
        public void run() {
            // todo auto-generated method stub
            seekbar.setprogress((mservice.getcurrentposition()*100)/mservice.getduration());
            mhandler.postdelayed(seekbarthread, 200);
        }
    };

}