There are a
lot of music players on Linux. Having gone through a fair share of them, I wrote this script to universally disambiguate my "change song" keybindings to the respective programs. It checks if they are running by doing a grep on
ps -A . It's pretty neat and extensible, and also installs itself appropriately.
#! /bin/bash
####################################################
# Music Player Song Change Script
# Handles many players at once
# mpchange {install|prev|play|pause|stop|next} (mplayer)
# Arun Chaganty <arunchaganty@gmail.com>
####################################################
MP_PATH="~/scripts"
. $MP_PATH/mpchange.conf # Contains app-specific info
if [[ $1 == "install" ]]; then
#Set commands
EDIT="gconftool-2 -t string -s /apps/metacity/keybinding_commands";
`$EDIT/command_1 "$MP_PATH/mpchange prev"`;
`$EDIT/command_2 "$MP_PATH/mpchange play"`;
`$EDIT/command_3 "$MP_PATH/mpchange pause"`;
`$EDIT/command_4 "$MP_PATH/mpchange stop"`;
`$EDIT/command_5 "$MP_PATH/mpchange next"`;
#set keybinding
EDIT="gconftool-2 -t string -s /apps/metacity/global_keybindings";
`$EDIT/run_command_1 $PREV`;
`$EDIT/run_command_2 $PLAY`;
`$EDIT/run_command_3 $PAUSE`;
`$EDIT/run_command_4 $STOP`;
`$EDIT/run_command_5 $NEXT`;
exit;
elif [[ $1 == "prev" || $1 == "play" || $1 == "pause" || $1 == "stop" || $1 == "next" ]]; then
for mp in $MPLAYERS; do
if [[ $mp[0] == "#" ]]; then continue; fi
if [[ `pgrep $mp$` ]]; then
"$mp"-change $1
exit;
fi
done
exit;
else [[ $1 == "help" ]];
echo "mpchange: Music Player Track Changer";
echo "Handles: $MPLAYERS";
echo "usage: mpchange {install|prev|play|pause|stop|next} (mplayer)";
exit;
fi
Here is a sample config file, with the commands for some common players. Adding another player is pretty direct. The keybindings are inspired from Amarok, Super+
key.
## Configuration file for mpchange
MPLAYERS="mocp totem banshee-1 rhythmbox audacious #listen #xmms" #if more than one is open, it follows the order. #<name> comments out the player.
# keybindings:
PREV="<Super>z"
PLAY="<Super>x"
PAUSE="<Super>c"
STOP="<Super>v"
NEXT="<Super>b"
# app-specific bindings:
# Pretty straightforward to add too
# Just type in "<player-name> --help" in the console
# Search the output for anything relevant. Some players
# may not support this feature (eg. Rhythmbox). There
# may be other methods of doing it then.
#
function rhythmbox-change {
case "$1" in
prev) # Rhythmbox's single previous goes to the beginning of the song.
`rhythmbox-client --previous`;
;;
play) # Play doesn't really do anything when playing.
`rhythmbox-client --play`;
;;
pause)
`rhythmbox-client --play-pause` #Similar to play-pause
;;
stop) # listen not have stop per se. Work around.
`rhythmbox-client --pause`
;;
next)
`rhythmbox-client --next`
;;
*)
echo "rhythmbox does not have the feature: $1"
exit
;;
esac;
}
function listen-change {
case "$1" in
prev) # Banshee's single previous goes to the beginning of the song.
`listen --previous` && `listen --previous`
;;
play) # Play doesn't really do anything when playing.
`listen --previous`;
;;
pause)
`listen --play-pause` #Similar to play-pause
;;
stop) # listen not have stop per se. Work around.
`listen --pause`
;;
next)
`listen --next`
;;
*)
echo "listen does not have the feature: $1"
exit
;;
esac;
}
function banshee-1-change {
case "$1" in
prev) # Banshee's single previous goes to the beginning of the song.
`banshee --previous`
;;
play)
`banshee --play`
;;
pause)
# Oddly, this doesn't show up on the console list.
`banshee --toggle-playing`
;;
stop)
# After using --stop, playback continues on the *next* song.
`banshee --pause`
;;
next)
`banshee --next`
;;
*)
echo "banshee does not have the feature: $1" exit
;;
esac;
}
function audacious-change {
case "$1" in
prev)
`audacious --rew`
;;
play)
`audacious --play`
;;
pause)
`audacious --play-pause`
;;
stop)
`audacious --stop`
;;
next)
`audacious --fwd`
;;
*)
echo "audacious does not have the feature $1"
exit
;;
esac;
}
function xmms-change {
case "$1" in
prev)
`xmms --rew`
;;
play)
`xmms --play`
;;
pause)
`xmms --play-pause`
;;
stop)
`xmms --stop`
;;
next)
`xmms --fwd`
;;
*)
echo "xmms does not have the feature $1"
exit
;;
esac;
}
function totem-change {
case "$1" in
prev)
`totem --previous`
;;
play)
`totem --play`
;;
pause)
`totem --play-pause`
;;
stop)
`totem --pause` #For some reason, totem's pause is same as stop
;;
next)
`totem --next`
;;
*)
echo "totem does not have the feature: $1"
exit
;;
esac;
}
function mocp-change {
case "$1" in
prev)
`mocp --previous`
;;
play)
if [[ `mocp -i` == "State: STOP" ]]; then
`mocp --play`;
else
`mocp --unpause`;
fi
;;
pause)
`mocp --toggle-pause`
;;
stop)
`mocp --pause` #For some reason, mocp's stop puts you back at the start of the playlist
;;
next)
`mocp --next`
;;
*)
echo "mocp does not have the feature: $1"
exit
;;
esac;
}
There is one comment on this page. [Display comment]