1.在manifests.xml中添加
<service android:name=".utils.MusicService" android:enabled="true"/>
2.新建一个音乐类
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
/**
* MusicService 控制后台音乐播放
* 提供控制方法,在Activity中调用
*/
/*
public class MusicService extends Service implements MediaPlayer.OnErrorListener {
private final IBinder mBinder = new ServiceBinder(); //在Activity 使用Ibinder 对象与 MusicService 进行交互
MediaPlayer mediaPlayer;
private int length = 0;
public MusicService() {
//这里可以执行一些初始化
}
public class ServiceBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.bg_main);
mediaPlayer.setOnErrorListener(this);
if (mediaPlayer != null) {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
}
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int
extra) {
onError(mediaPlayer, what, extra);
return true;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer.start();
return START_STICKY;
}
public void pauseMusic() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
length = mediaPlayer.getCurrentPosition();
}
}
public void resumeMusic() {
if (mediaPlayer.isPlaying() == false) {
mediaPlayer.seekTo(length);
mediaPlayer.start();
}
}
public void stopMusic() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public boolean stopService(Intent name) {
mediaPlayer.stop();
mediaPlayer.release();
return super.stopService(name);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
try {
mediaPlayer.stop();
mediaPlayer.release();
} finally {
mediaPlayer = null;
}
}
}
public boolean onError(MediaPlayer mp, int what, int extra) {
Common.showShort(this,String.valueOf(R.string.music_player_fail));
if (mediaPlayer != null) {
try {
mediaPlayer.stop();
mediaPlayer.release();
} finally {
mediaPlayer = null;
}
}
return false;
}
/*调用说明:
开始、暂停、继续播放、停止 音乐:
step 1:在 activity 的onCreate 中调用 doBindService 来将 service 绑定到 activity
Step 2: 通过Intent 启动service
Intent music = new Intent();
music.setClass(this,MusicService.class);
startService(music);
Step 3: 在 activity中调用 pause, resume 或者 stop 来控制音乐:
mServ.pauseMusic();
mServ.resumeMusic();
mServ.stopMusic();
Step 4: 在Activity 的onDestroy 中调用 doUnbindService
以下内容写在Activity中:
*/
private boolean mIsBound = false;
private MusicService mServ;
private ServiceConnection Scon =new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder
binder) {
mServ = ((MusicService.ServiceBinder)binder).getService();
}
public void onServiceDisconnected(ComponentName name) {
mServ = null;
}
};
void doBindService(){
bindService(new Intent(this,MusicService.class),
Scon,Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService()
{
if(mIsBound)
{
unbindService(Scon);
mIsBound = false;
}
}