pythonlinuxaudioresumepause

Pause and/or resume audio using Python on Linux


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.


Solution

  • 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

    (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")