我正在开发一个音乐应用程序。我想检测其他应用程序使用扬声器或闹钟铃声来播放/暂停音乐。
对于电话,我使用 PhoneStateListener。我尝试使用 OnAudioFocusChangeListener 来检测其他音频。但是没有用。
我想知道如何解决这个问题。
我解决了默认警报应用程序的问题:
public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";
public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";
private BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(ALARM_ALERT_ACTION) || action.equals(ALARM_DISMISS_ACTION) || action.equals(ALARM_SNOOZE_ACTION) || action.equals(ALARM_DONE_ACTION))
{
// for play/pause mediaplayer
playPause();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
IntentFilter filter = new IntentFilter(ALARM_ALERT_ACTION);
filter.addAction(ALARM_DISMISS_ACTION);
filter.addAction(ALARM_SNOOZE_ACTION);
filter.addAction(ALARM_DONE_ACTION);
registerReceiver(mReceiver, filter);
}