Android 应用程序:媒体应用程序在电话通话或响铃模式下不会静音

发布于 2024-12-19 19:52:15 字数 3364 浏览 2 评论 0原文

这是我的第一个自学项目..在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);   
}

我的活动类别是喜欢

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

请建议/帮助...

This is my first self taught project .. in this project in webview i am playing a flash video from my website...

I is working fine ... i mean its palying music etc no problem on that side..

only trouble is when i someone ring media player does not mute and in conversation i still listening to music..

i would like to mute media volume while in call or in ringing mode..then resume back after call finishes...

here are the codes i m using ( only the main part)

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

my activity class is like

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

Please advise/help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2024-12-26 19:52:15

音频焦点与您想要做的事情不同。

您需要一个 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);

Audio focus is a different thing from what you are trying to do.

You need a PhoneStateListener and monitor when a call comes and when it goes away. Something like this:

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

Then in your onCreate() or similar:

TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
mgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文