简体   繁体   中英

How can I programmatically determine if a device is connected to an audio jack?

Is there a way to determine in Ubuntu if there are devices plugged into the audio and microphone jacks?

I've searched for this and all the answers I've found have to do with detecting the action of plug or unplug. I need to know if it's possible to determine if the device is already connected. The reason is we do a lot of automated testing and some of our tests rely on devices being connected to the machines. We'd like our tests to be smart enough to detect a device or not so they can behave accordingly.

If there is a solution, I eventually hope to port it over to OS X and Windows, if possible.

Thanks

Assuming you really mean audio jack rather than USB device: Those are purely mechanical/electrical connections, and the audio hardware is not set up to report back whether anything is connected or not.

You can try monitoring for audio on the microphone and seeing if any background-noise signal is present. After that, you could try playing a tone, or set of tones, through the speakers and seeing if the microphone picks them up. (Which it may not if this is a headset, but for your test environment you should be able to specify what equipment gets hooked up.) But other than that sort of "try it and see", I think you're out of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM