Android应用程序:媒体应用程序在电话或铃声模式下不静音
这是我的第一个自学项目。。在webview中的这个项目中,我正在播放我网站上的flash视频 我工作很好。。。我的意思是演奏音乐等,在那方面没问题 唯一的问题是,当我打电话给媒体播放器时,它并没有静音,在对话中,我仍然在听音乐 我想在通话或铃声模式下静音媒体音量…然后在通话结束后恢复 以下是我仅使用的主要部分的代码Android应用程序:媒体应用程序在电话或铃声模式下不静音,android,android-audiomanager,Android,Android Audiomanager,这是我的第一个自学项目。。在webview中的这个项目中,我正在播放我网站上的flash视频 我工作很好。。。我的意思是演奏音乐等,在那方面没问题 唯一的问题是,当我打电话给媒体播放器时,它并没有静音,在对话中,我仍然在听音乐 我想在通话或铃声模式下静音媒体音量…然后在通话结束后恢复 以下是我仅使用的主要部分的代码 public void onAudioFocusChange(int focus) { //focus = audio.getMode(); switch(focus
public void onAudioFocusChange(int focus) {
//focus = audio.getMode();
switch(focus){
case AudioManager.MODE_RINGTONE :
Pause();
Toast.makeText(this, "PAUSE... RINGTONE", 15000).show(); //just need to see if this function really been called...
break;
case AudioManager.MODE_IN_CALL :
Pause();
Toast.makeText(this, "PAUSE.. IN CALL", 15000).show(); //just need to see if this function really been called...
break;
case AudioManager.MODE_IN_COMMUNICATION :
Pause();
Toast.makeText(this, "PAUSE... IN COMMUNICATION", 15000).show(); //just need to see if this function really been called...
break;
case AudioManager.MODE_NORMAL :
Resume();
Toast.makeText(this, "RESUME... NORMAL MODE", 15000).show(); //just need to see if this function really been called...
break;
}
}
public void Pause(){
audio.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
audio.setMode(AudioManager.MODE_IN_CALL);
audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
public void Resume(){
audio.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
audio.setMode(AudioManager.MODE_NORMAL);
audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
我的活动课是这样的
请提供建议/帮助…音频焦点与您正在尝试的不同 当一个电话来了和离开时,你需要一个电话和监视器。大概是这样的:
PhoneStateListener mPhoneStateListener = new PhoneStateListener()
{
protected boolean mWasPlayingWhenCalled = false;
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
if( state == TelephonyManager.CALL_STATE_RINGING )
{ // Incoming call: Pause music
if( mPlaying )
{
stop();
mWasPlayingWhenCalled = true;
}
}
else if(state == TelephonyManager.CALL_STATE_IDLE )
{ // Not in call: Play music
if( mWasPlayingWhenCalled )
{
start();
mWasPlayingWhenCalled = false;
}
}
else if( state == TelephonyManager.CALL_STATE_OFFHOOK )
{ // A call is dialing, active or on hold
}
super.onCallStateChanged(state, incomingNumber);
}
};
然后在onCreate或类似程序中:
TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
mgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
音频聚焦与您正在尝试的是不同的事情 当一个电话来了和离开时,你需要一个电话和监视器。大概是这样的:
PhoneStateListener mPhoneStateListener = new PhoneStateListener()
{
protected boolean mWasPlayingWhenCalled = false;
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
if( state == TelephonyManager.CALL_STATE_RINGING )
{ // Incoming call: Pause music
if( mPlaying )
{
stop();
mWasPlayingWhenCalled = true;
}
}
else if(state == TelephonyManager.CALL_STATE_IDLE )
{ // Not in call: Play music
if( mWasPlayingWhenCalled )
{
start();
mWasPlayingWhenCalled = false;
}
}
else if( state == TelephonyManager.CALL_STATE_OFFHOOK )
{ // A call is dialing, active or on hold
}
super.onCallStateChanged(state, incomingNumber);
}
};
然后在onCreate或类似程序中:
TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
mgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);