开发者

Android headphone detection

开发者 https://www.devze.com 2023-02-15 22:00 出处:网络
I have an app that will speak text messages to the user. I want to make it so that when the user hits a button called \"H开发者_JS百科eadphones on\" the app will only speak to it when headphones are d

I have an app that will speak text messages to the user. I want to make it so that when the user hits a button called "H开发者_JS百科eadphones on" the app will only speak to it when headphones are detected. Is there a command that will allow me to detect if headphones are plugged in or not?


There is a broadcast made when the headphones are plugged in: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG

You need to register a BroadcastReceiver to receive this and perform your required actions.


It seems in this case you just want to check if headphone are connected before to start the audio playout, so you should use audioManager.isWiredHeadsetOn() like this:

AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
    // Play audio...
}
0

精彩评论

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