1

在我的应用程序中,我在服务中使用媒体播放器来播放音乐文件。我用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
    });
4

2 回答 2

1

我已经解决了我的问题。下面,我发布了我的音乐服务的代码。

public class ChalisaService extends Service implements OnCompletionListener
{
static MediaPlayer mediaPlayer;
static int playerFlag = 0;
TelephonyManager tm;
ActivityManager actManager;
/**
 * 0 for stop/pause
 * 1 for play*/
@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);
    mediaPlayer.setOnCompletionListener(this);
    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 = 0;
    }//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() 
{
    super.onDestroy();
    /*//mediaPlayer.stop();
    //mediaPlayer.release();
    playerFlag = 0;
    Log.v("Chalisa service", "on destroy called");*/
}//onDestroy

private PhoneStateListener mPhoneListener = new PhoneStateListener() {
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            if(mediaPlayer.isPlaying())
            {
                mediaPlayer.pause();
                playerFlag = 0;
            }//if
            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:
            if(mediaPlayer.isPlaying())
            {
                mediaPlayer.pause();
                playerFlag = 0;
            }//if
            break;

            /**
             * Nitish 
             * 26 Sep 2012, Wed 
             * 11:50 AM*/
        case TelephonyManager.CALL_STATE_IDLE:
            if(!mediaPlayer.isPlaying())
            {
                mediaPlayer.start();
                playerFlag = 1;
            }//if
        default:
            Log.d("Chalisa Service", "Unknown phone state=" + state);
        }
    } 
};

public void onCompletion(MediaPlayer mp) 
{
    mp.stop();
    mp.release();
    playerFlag = 0;
    stopSelf();
    updateUI();
    Log.v("Chalisa Service media player", "on completion listener called");
}

private void updateUI() 
{
    Intent in = new Intent("com.dzo.HanumanChalisaWithAudioAndAlarm.UPDATE_UI");
    in.putExtra("Player_FLAG_VALUE", playerFlag);
    getApplicationContext().sendBroadcast(in);
}
}//ChalisaService
于 2012-09-26T16:13:37.767 回答
0

尝试使用以下命令强制它暂停:

Intent i = new Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "pause");
        mContext.sendBroadcast(i);
于 2012-09-25T11:22:28.383 回答