How to create a keyboard shortcut to play/pause any audio/video running in the background?

6,492

Solution 1

If you only want to control rhythmbox then rhythmbox-client --play to play the song or rhythmbox-client --pause to pause the song should be enough.

But I found this software called playerctl in some Arch wiki which control the currently playing media. I'm not 100% sure what its priority is, but when I have a YouTube video open it controls that by default rather than messing with Rhythmbox. To toggle between play and pause, run playerctl play-pause. To go to the next vid/music you run playerctl next and for the previous vid/music you run playerctl previous.

I'm not 100% sure if this works on any other distro or if it's even available, but it works for me on Arch.

Solution 2

Rhythmbox seems to support both rhythmbox-client --play/--pause commands, and D-Bus commands sent using dbus-send, as explained in this question on AskUbuntu and this question here on U&L.SE as linked from that AskUbuntu answer.

In short, the dbus-send command would be:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.rhythmbox /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

with the condition that Rhythmbox's MPRIS API needs to be enabled first.

But if you need to also control any YouTube videos using those same keypresses, you'll probably need those keypresses to activate a script that figures out which player is currently doing the playing, and sends the right commands either to Rhythmbox or to whichever web browser (or other app) you're using to play YouTube videos. And for the browser, the command/keypress would need to be sent for the correct browser tab, which won't necessarily be the current one...

Share:
6,492
Mayank Shigaonker
Author by

Mayank Shigaonker

Updated on September 18, 2022

Comments

  • Mayank Shigaonker
    Mayank Shigaonker almost 2 years

    So I bought this keyboard for my laptop that doesn't have media keys on it and I am very used to pressing the play/pause button on my laptop to pause any media playing in the background. With this keyboard however I have to create my own keyboard shortcuts to do that because of the absence of media keys. Till now I have managed to make keyboard shortcuts for volume control.

    Research and my attempts:

    1. I found this page which shows a command to simulate media keys from the terminal. https://askubuntu.com/questions/235126/simulate-media-keys-in-terminal/235181#235181. The command is xdotool key XF86AudioPlay but when I make a keyboard shortcut out of this by going to Settings > Keyboard > Application Shortcuts. It doesn't seem to work.

    2. When I press the keyboard shortcut which I have set to Super+F11 my CPU usage rises a bit which means that the shortcut is working. I confirmed this by changing the command the shortcuts executes to exo-open --launch WebBrowser which as you might have thought opens my web browser when I press Super+F11.

    3. Now because I knew a bit of C/C++ I created this wrapper program which does exactly the same thing

    #include <stdlib.h>
    
    int main() {
        system("xdotool key XF86AudioPlay");
    }
    

    and mapped the shortcut to execute this program but Alas!! it still doesn't work.

    How do I get the command that XFCE executes when I press the default play/pause button on my laptop keyboard because that seems to play/pause any media playing in the background.

    I use XFCE on Arch Linux. I usually listen to music on rhytmbox music play.

    • telcoM
      telcoM almost 4 years
      It sounds like your XFCE does not currently know that it needs to send an appropriate play/pause command to your media player of choice, when it receives the media key event. Once you know how your media player expects to have those commands sent, you should set your (global) keyboard shortcuts to send those commands instead of trying to simulate media keys which aren't necessarily configured to do the right thing by default. Since you did not identify which media player you're using, it is going to be difficult to offer any more advice than that.
    • Mayank Shigaonker
      Mayank Shigaonker almost 4 years
      @telcoM I think I have answered your comment in the edit I made to the my question.
    • Quasímodo
      Quasímodo almost 4 years
      You don't need to simulate XF86AudioPlay to get the application to hear your keypresses. What you need is to write a script that will send a command of your media player that controls the play/pause. But we need to know the media player. For example, with cmus, cmus-remote -u is what toggles play/pause.
  • Mayank Shigaonker
    Mayank Shigaonker almost 4 years
    I think I got how XFCE does the play/pause. In the xfce4-pulseaudio-plugin there are two options for playing/pausing anything on rhythm box or vlc media player and all it does it toggle between these when I press the key (Yup it doesn't pause youtube videos. Sorry that was a wrong assumption). Now I'll re-create this functionality with a C++ program. Thanks!!
  • Mayank Shigaonker
    Mayank Shigaonker over 3 years
    @Jess sorry I really forgot about this post 😅. I've updated it with the answer I found to the problem. Good Luck :)