当电话铃声响起,收到短信如何暂停音乐等?电话铃、声响起、短信、音乐

由网友(凉辰梦瑾空人心)分享简介:我试图探测机器人的声音,并暂停我的应用程序的音乐(暂时或永久),而他们制造噪音。I'm trying to detect android sounds and pause my app's music (temporarily or permanently) while they make noise.这code显...

我试图探测机器人的声音,并暂停我的应用程序的音乐(暂时或永久),而他们制造噪音。

I'm trying to detect android sounds and pause my app's music (temporarily or permanently) while they make noise.

这code显得无能为力(停止的暂停音乐和 P 的写入日志),它永远不会被调用:

This code appears to do nothing (stop pauses the music and p writes to the log), it never gets called:

public class PollyPrissyPants extends Activity implements OnAudioFocusChangeListener {
    // Blah blah blah
    public void onAudioFocusChange(int mal) {
        p("--CHANGE!!!--" + mal);
        stop();
    }
    // Yada yada yada
}

我必须将其设置在其他地方呢?是@覆盖相关?

Do I have to set it up somewhere else as well? Is @Override relevant?

我还没有试过PhoneStateListener但如果可能,我不希望有单独对待电话,报警,通知,游戏等。我使用了震动,但现在它吮吸作为解决方案。如果我叫,我来回答,然后迅速得到我的应用程序,并暂停。

I haven't tried PhoneStateListener but if possible I don't want to have to treat phone calls, alarms, notifications, games, etc separately. I'm using vibrate for now but it sucks as a solution. If I get called, I have to answer and then quickly get to my app and pause it.

推荐答案

您必须注册 OnAudioFocusListener AudioManager

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.requestAudioFocus(this,
      AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

这个参数是实施 OnAudioFocusListener 类的引用。

The this parameter is a reference to the class implementing OnAudioFocusListener.

阅读全文

相关推荐

最新文章