I've just installed Spotify on my Ubuntu 20.04 Gnome 3.36.8 and I've found out that the media keys on my keyboard can't control Spotify. They control other media players just fine, only Spotify is not working. People say that this is a Spotify bug, but until they solve it I need it to be working.
I have found this post proposing to use a custom key binding to control Spotify. https://fabianlee.org/2016/05/25/ubuntu-enabling-media-keys-for-spotify/ This works! Except that to make it work I needed to disable the default behavior, thus I can control Spotify but I loose control over the other media players.
Then, I had the idea to use the same concept "use a custom command" but this time it will control all opened media players. Searching online I have found these other posts, they all helped me in my solution.
How to get current status of a media player using python https://stackoverflow.com/questions/23324841/using-org-mpris-mediaplayer2-player-playbackstatus-property-in-python
How to send commands to a media player using python https://stackoverflow.com/questions/67727438/how-do-i-pause-a-sound-song-using-python
How to create a custom binding key to run a python script https://askubuntu.com/questions/329710/bind-command-lines-to-keys
The end solution consists of creating a python script that takes an action as input, the action is each of my media keys: PlayPause, Previous, Next. I don't have a different button for Play and another for Pause, it is the same button, but in my script I put one function just for Play and another just for Pause if that is your case. As I have one single button PlayPause, my implementation first checks if there is any media current playing and pause it. Only if there is no media playing then it executes the Play method. I did this because it is just the way I like to use my players, feel free to change it for your needs.
Then, create bash scripts to execute each of the funtions to call the python script. To disable current key bindings I used GUI (sorry, I don't have a script for those, if I can find a way, I'll update this gist). First, Settings --> Keyboard shortcuts --> Sound and media --> disable PlayPause, Previous, Next Second, use dconf-editor. Search for any play/pause/previous/next, disable the default value and use [''] for all.
Then, run the last script to create the bindings using gsettings.