萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Activity與Service之間交互並播放歌曲的實現代碼

Activity與Service之間交互並播放歌曲的實現代碼

以下是對Activity與Service之間交互並播放歌曲的實現代碼進行了詳細的分析介紹,需要的朋友可以過來參考下  

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;
        }
    &

copyright © 萬盛學電腦網 all rights reserved