How To Get Global Media Shortcuts in KDE

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Do not start a support topic here please. Before you post please read this

How To Get Global Media Shortcuts in KDE

Postby NarcT on Tue Apr 17, 2012 9:56 pm

One thing that bugged me about KDE was the lack of Global Media Shortcuts with music/video players. If you want a key for the Play/Pause, Next, Previous, or Stop functions you might have noticed they are assigned to Amarok by default. Well I wanted to be able to pause my music no matter if it was playing through Amarok, Banshee, Pithos, or any other player.

One caveat at the moment, this only works with applications that support MPRIS or "Media Player Remote Interfacing Specification". A large amount of media applications already support this but if you aren't sure open your application and type this code in a terminal.
Code: Select all
qdbus | grep org.mpris.MediaPlayer2 | sed 's/.*\.//'


If you see your apps name after typing this command into the terminal then it will work. Make sure your application is open before you run this command.

[Overview]
The overview of my fix:
Say I want to be able to set the Play/Pause button to work on any media player allowing me to play or pause the music. First create a script to send the the Play/Pause command to your applications.

Here's my script for Play/Pause:
Code: Select all
#!/bin/bash

## -- This script will imitate Gnome's Media Controls (Play/Pause, Next, Previous, Stop) -- ##
## -- It will assume you are using a media application that is compatible with MPRIS or  -- ##
## -- "Media Player Remote Interfacing Specification"                                    -- ##

# Search for running media applications
# and store into apps array
apps=(`qdbus | grep org.mpris.MediaPlayer2 | sed 's/.*\.//'`)

# For each application send the "PlayPause" command
for app in "${apps[@]}"
  do   
        qdbus org.mpris.MediaPlayer2.$app /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  done


If you notice the actual command is specified at the end of the 2nd to last line. This "PlayPause" instructs the application to do what we want. But what if we want to add a Next Song key? Just copy this script and change the "PlayPause" to "Next". Pretty simple.

Now all we have to do is assign this script to a global shortcut. In KDE go to System Settings > Shortcuts and Gestures > Custom Shortcuts. Right click in the white space and select New > Global Shortcut > Command URL. Name your Action whatever you like and now click the Trigger tab and assign your appropiate shorcut. Now click the Action tab and browse to your previously made script. Make sure your script is executable by quickly typing "chmod +x /home/USER/script". After you click Apply you should be able to control all supported media applications with your shortcut!
NarcT
Level 1
Level 1
 
Posts: 4
Joined: Tue Apr 17, 2012 9:37 pm

Linux Mint is funded by ads and donations.
 

Re: How To Get Global Media Shortcuts in KDE

Postby digitsm on Mon Jun 10, 2013 1:18 am

Yeees! That worked for me even in Xfce. With this method you can Play/Pause/Next/Previous your music in EVERY desktop environment and with EVERY music player.
However the final bash script I used was a little different with NarcT's, as follows:
Code: Select all
#!/bin/sh
## -- This script will imitate Gnome's Media Controls (Play/Pause, Next, Previous, Stop) -- ##
## -- It will assume you are using a media application that is compatible with MPRIS or  -- ##
## -- "Media Player Remote Interfacing Specification"                                    -- ##

# Search for running media applications
# and store into apps array
# *I used NO parentheses
apps=`qdbus | grep org.mpris.MediaPlayer2 | sed 's/.*\.//'`

# For each application send the "PlayPause" command
# *I used apps instead of apps[@]
for app in "${apps}"
  do   
        qdbus org.mpris.MediaPlayer2.$app /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  done


The only problem is that I had to use qdbus which is based on Qt. Because Xfce uses Gtk+2 this eliminates the purity of Xfce. (I couldn't find an equivalent for qdbus in Xfce)
If you know an assured solution/idea/suggestion, I'll be glad to send it to me @?
digitsm at gmail/yahoo dot com
User avatar
digitsm
Level 1
Level 1
 
Posts: 8
Joined: Thu Jan 06, 2011 10:48 am
Location: Tehran


Return to Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 15 guests