2

thank you for your attention! I want to pause and/or resume audio which is being played at the background using python, found this way, which uses pynput:

from pynput.keyboard import Controller, Key

c = Controller()
c.press(Key.media_play_pause)

But audio is still playing. Seems there are no errors, but it doesn't work.

Michael
  • 657
  • 4
  • 29

1 Answers1

1

You might want to try using playerctl, a command line tool that you could use with

subprocess.call(("playerctl", "play-pause"))

However, playerctl might not come preinstalled on your system, so you might have to

  • sudo apt install playerctl,
  • pacman -Syu playerctl,
  • sudo dnf install playerctl or
  • sudo zypper install playerctl

(depending on your distro)

This would not require pynput, but subprocess (import subprocess). If you don't care about style and security you could of course also use os.system("playerctl play-pause")

charlie
  • 389
  • 3
  • 16
  • 1
    It pauses, and plays. Thank you! I often use telegram while working, that's why i needed to control the music via bot, so i wouldn't switch windows, turn on/off and go back! Thank you! – Michael May 31 '20 at 19:47