简体   繁体   English

Python –检查Mac是否正在播放音乐

[英]Python – Check if Mac is Playing Music

I'm trying to write a simple python script to stop the music being played by a Mac. 我正在尝试编写一个简单的Python脚本来停止Mac播放的音乐。 I found some code that emulates the media buttons from the accepted answer here: emulate media key press on Mac . 我在此处找到了一些模拟媒体按钮的代码: 在Mac上模拟媒体按键

Triggering the play/pause button works perfectly, but I only want to do so if there is music currently playing. 触发播放/暂停按钮效果很好,但是我只想在当前正在播放音乐的情况下这样做。 Otherwise it turns on the music (the opposite of what I'm trying to do. Is there any way to get this information from the system? 否则,则会开启音乐(什么我试图做相反的事情。有什么办法来从系统得到这些信息?

I need to check if music was actually playing beforehand so I can know whether to resume it later. 我需要事先检查音乐是否真的在播放,这样我才能知道是否稍后再播放。

If your use case is macOS specific, you can call AppleScript via Python: 如果您的用例是特定于macOS的,则可以通过Python调用AppleScript:

import subprocess
subprocess.call(['osascript', '-e', 'tell application "iTunes" to pause'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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