I would like to pause the music / audio of chrome. for example when I receive a specific input the audio of a video stops and then restarts when it receives a specific input, what should I use?
Asked
Active
Viewed 581 times
1 Answers
2
You can use something called dbus, and there is even a python library for it.
You will first have to find the name of the media player using the following code.
import dbus
for service in dbus.SessionBus().list_names():
if ".Media" in service:
print(service)
You will get a list something like this
org.mpris.MediaPlayer2.spotify
org.mpris.MediaPlayer2.chromium.instance1234
...
In the things printed, find the one that looks to be your browser.
Then the easiest way to run this command is just using subprocess.
import subprocess
subprocess.run(
[
"dbus-send",
"--print-reply",
"--dest=org.mpris.MediaPlayer2.chromium.instance1234",
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player.PlayPause",
],
)
After that, it should pause or play the song.
Note: You don't just have to do PlayPause
, there is also Previous
and Next
and Pause
.
If you want to use input, you can do something like this
import subprocess
def pause_play():
subprocess.run(
[
"dbus-send",
"--print-reply",
"--dest=org.mpris.MediaPlayer2.chromium.instance63145",
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player.PlayPause",
],
)
if __name__ == "__main__":
while 1:
command = input("Enter a command: ")
if command == "p":
pause_play()

Jake
- 81
- 1
- 6
-
I don't find dbus on windows 10, solution? – francesco diploma May 28 '21 at 16:33
-
@francescodiploma You can make a dbus server, here is an example https://stackoverflow.com/questions/22390064/use-dbus-to-just-send-a-message-in-python – Jake May 28 '21 at 17:56
-
1There are more options such as: next, previous, stop, seek, setposition etc. MediaPlayer2.Player manual here: https://specifications.freedesktop.org/mpris-spec/2.2/Player_Interface.html – zeroalpha Jan 10 '23 at 09:48
-
@zeroalpha thank you for the source. Good addition. – Jake Jan 10 '23 at 19:37