【问题标题】:How to detect if headphones was disconnected while media is playing [duplicate]如何在媒体播放时检测耳机是否断开[重复]
【发布时间】:2020-09-03 00:43:07
【问题描述】:

如何检测当前播放媒体时耳机是否断开连接......是否有状态监听器?

以及如何检测耳机何时连接?

【问题讨论】:

  • 是的,瑞恩,非常感谢!我一上午都在找!非常感谢!

标签: java android-studio


【解决方案1】:

根据this link,您可以创建一个BroadcastReceiver,用于检查耳机插入或拔出时发送的意图。

broadcastReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
               pluggedState = intent.getIntExtra("state", -1);
               if (pluggedState == 0) {
                  // headset not plugged in
               }
               if (pluggedState == 1) {
                  // headset plugged in
               }
            }
         }
};

请注意,Intent.ACTION_HEADSET_PLUG 是通过粘性广播发送的,这意味着一旦注册了广播接收器(可能在活动启动时),您将收到上次更新意图时的值。详情请见this question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多