[英]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. 现代Linux系统使用dbus消息传递系统在应用程序之间进行通信。 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).
您可以操纵现有对象(这意味着:其他正在运行的软件),也可以收听一些有关系统更改的信号(屏幕锁定,插入USB驱动程序,插入耳机等)。 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
. 您可以使用dbus-python库与
python
dbus
通信。 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. 您需要找到的是问题的答案:如何使用dbus向桌面管理器询问耳机等已连接外围设备的状态。 There is a chance it is documented somewhere on freedesktop website.
有可能在freedesktop网站上的某处记录了该文档。 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).
但是,如果您决定在某个论坛上提问,请不要忘记提及您的桌面管理器(kde,gnome等)。 BTW, I'm not certain is such question would be on-topic here, on Stack Overflow, but Ask Ubuntu shall be fine.
顺便说一句,我不确定这样的问题是否会在这里出现在Stack Overflow上,但是Ask Ubuntu应该会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.