简体   繁体   English

python脚本如何检测耳机/耳机是否已连接到PC?

[英]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.

相关问题 如何以编程方式检测文件是否为 Python 脚本 - How to Programmatically detect whether a file is a Python script 如何在使用蓝牙耳机时使用 python-sounddevice 录制系统音频? - How can I record system audio using python-sounddevice while using bluetooth earphones? 使用 Python,如何检测程序是最小化还是最大化? - Using Python, how can I detect whether a program is minimized or maximized? Python,如何检测电脑是否处于电池供电状态? - In Python, how can I detect whether the computer is on battery power? 如何检测两个盒子是否相互连接? - How to detect whether two boxes are connected with each other or not? 如何检测 python 上连接的新 usb 设备 - How to detect a new usb device is connected on python Python:服务器端检测用户是否登录到Facebook(不一定连接到我的应用程序) - Python: server side detect whether user is logged in to facebook (and not necessarily connected to my app) 如何检测是否已达到Python子进程的超时时间? - How can I detect whether the timeout in Python's subprocess has been reached? 如何检测我的python类实例是强制转换为整数还是浮点数? - How can I detect whether my python class instance is cast as an integer or float? 如何使用 python 脚本运行战车? “此应用程序无法在您的 PC 上运行” - How to run Chariot using python script? "This app can't run on your PC"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM