开发者

How to pause music when phone rings, sms is received, etc?

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

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?

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开发者_StackOverflow社区 to my app and pause it.


You have to register the OnAudioFocusListener with the AudioManager:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜