简体   繁体   中英

How can a python script detect whether headphones/earphones are connected to the pc or not?

如果尝试在未插入耳机的情况下播放音乐,我想编写一个python发出警告。如何检测它们是否已连接?

Modern Linux systems use dbus messaging system to communicate between application. You can manipulate existing objects (that means: other running software) or you can listen to some signals telling about changes in the system (screen locked, USB driver inserted, headphones plugged in, etc). Depending on the interface provided by system components (to be exact: mostly by your desktop manager components) you can also ask about current state. You can use dbus-python library to communicate with dbus from python . That's the software development part.

What you need to find is the answer to question: how to ask desktop manager using dbus about the state of connected peripherals like headphones. There is a chance it is documented somewhere on freedesktop website. They aim at standardize the way components communicate with each other. So, there is high chance your solution would be desktop manager independent. However, if you decide to ask this question on some forum, please do not forget to mention your desktop manager (kde, gnome, etc). BTW, I'm not certain is such question would be on-topic here, on Stack Overflow, but Ask Ubuntu shall be fine.

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