gpt4 book ai didi

安卓应用 : Media app not muting while in PHONE call or in ringing mode

转载 作者:行者123 更新时间:2023-11-29 16:18:34 28 4
gpt4 key购买 nike

这是我的第一个自学项目..在这个项目中,我在 webview 中播放我网站上的 flash 视频...

我工作正常...我的意思是播放音乐等方面没问题..

唯一的麻烦是当我有人响铃时媒体播放器没有静音并且在谈话中我还在听音乐..

我想在通话中或处于响铃模式时将媒体音量静音..然后在通话结束后恢复...

这是我使用的代码(只有主要部分)

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

我的 Activity 课是这样的

public class MyActivity extends Activity implements OnAudioFocusChangeListener, OnSeekBarChangeListener {

private static final String MY_AD_UNIT_ID = "a14xxxxx";
private AdView adView;
private WebView myWebView;
private AudioManager audio;
private SeekBar sb;
private int result = AudioManager.MODE_NORMAL;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
audio =(AudioManager) MyActivity.this.getSystemService(Context.AUDIO_SERVICE);
//result = AudioManager.MODE_NORMAL;
result = audio.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
while(audio.isMusicActive())
{
this.onAudioFocusChange(result);

}

//control SeekBar
int maxV = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curV = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
sb = (SeekBar) findViewById(R.id.sb);
sb.setMax(maxV);
sb.setProgress(curV);
sb.setOnSeekBarChangeListener(this);

myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
myWebView.loadUrl("http://www.testpage.co.uk/live.html");
// Create the adView
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
// Lookup your LinearLayout
LinearLayout myAdview = (LinearLayout) findViewById(R.id.adsDisplay);
// Add the adView to it
myAdview.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();

adView.loadAd(request);
}

请指教/帮助...

最佳答案

音频焦点与您尝试做的事情不同。

你需要一个 PhoneStateListener并监控调用何时到来和何时消失。像这样:

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

关于安卓应用 : Media app not muting while in PHONE call or in ringing mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386536/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com