在我的应用程序中,我在服务中使用媒体播放器来播放音乐文件。我用TelephonyManager
它来检查来电和去电,并相应地播放和暂停音乐。
对于来电和去电,该应用程序可以正常工作,并且每当通话发生时音乐都会暂停,并在通话断开后恢复。但是,当我在通话断开后尝试手动暂停时,它不会暂停。
下面是我的代码:
public class ChalisaService extends Service
{
static MediaPlayer mediaPlayer;
static int playerFlag = 0;
TelephonyManager tm;
ActivityManager actManager;
Context mContext = getApplicationContext();
/**
* 0 for stop
* 1 for play
* 2 for pause*/
@Override
public IBinder onBind(Intent intent)
{
return null;
}//onBind
@Override
public void onCreate()
{
super.onCreate();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.chalisa);
mediaPlayer.setVolume(100, 100);
tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}//onCreate
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
/*HanuAlarm.txtPlay.setText("Play");
HanuAlarm.btn_Play.setBackgroundResource(R.drawable.btnplay);*/
playerFlag = 2;
}//if
else
{
mediaPlayer.start();
/*HanuAlarm.txtPlay.setText("Pause");
HanuAlarm.btn_Play.setBackgroundResource(R.drawable.btnpause);*/
playerFlag = 1;
}//else
startForeground(0, null);
return playerFlag;
}//onStartCommand
@Override
public void onDestroy()
{
mediaPlayer.stop();
mediaPlayer.release();
playerFlag = 0;
}//onDestroy
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
mContext.sendBroadcast(i);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Intent in = new Intent("com.android.music.musicservicecommand");
in.putExtra("command", "pause");
mContext.sendBroadcast(in);
break;
case TelephonyManager.CALL_STATE_IDLE:
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
playerFlag = 1;
}//if
else
{
Intent in1 = new Intent("com.android.music.musicservicecommand");
in1.putExtra("command", "pause");
mContext.sendBroadcast(in1);
}//else
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
default:
Log.d("Chalisa Service", "Unknown phone state=" + state);
}
}
};
}//ChalisaService
我正在播放或暂停媒体播放器的按钮代码:
btn_Play.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(ChalisaService.playerFlag == 0 || ChalisaService.playerFlag == 2)
{
startService(in);
ChalisaService.playerFlag = 1;
Log.i("HanuAlarm play button if", ""+chalisaPlaying);
txtPlay.setText("Pause");
txtPlay.setTextColor(getResources().getColor(R.color.redwine));
btn_Play.setBackgroundResource(R.drawable.btnpause);
}//if
else if(ChalisaService.playerFlag == 1)
{
ChalisaService.mediaPlayer.pause();
ChalisaService.playerFlag = 2;
Log.i("HanuAlarm play button else", ""+chalisaPlaying);
txtPlay.setText("Play");
txtPlay.setTextColor(getResources().getColor(R.color.white));
btn_Play.setBackgroundResource(R.drawable.btnplay_a);
}//else if
}//onClick
});