欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-02-27 14:29:38|栏目:Android代码|点击:

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

}

上一篇:android module解耦组件化总体概述(推荐)

栏    目:Android代码

下一篇:Android中使用adb命令通过IP地址连接手机

本文标题:Activity与Service之间交互并播放歌曲的实现代码

本文地址:http://www.codeinn.net/misctech/70607.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有