Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-20-2004, 01:17 AM   #1
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Rep: Reputation: 31
Play/Pause Script for MPD

I've got a strange problem with which maybe someone can help me. My keyboard has a Play/Pause button, and I'd like to be able to use it as it's intended. I use MPD as my music player, and also have mpc installed. I'd like to create a simple script that determines if MPD is currently playing a song, and if it is, pause the song, and if it's not (if it's stopped or paused), then play the song.
If I can get a script that'll do that, then I can set my Play/Pause button to execute that, because "mpc play" only plays and "mpc pause" only pauses.
I'm not a programmer, and don't know any programming languages (although I used to make simple programs on my Commodore64 in Basic. That was a long time ago, though). But I can learn a few simple commands easily enough. Mostly I just need to know if anyone has any ideas on how to determine if mpd is currently playing or not. I think I can figure out the rest after that.
Old 12-20-2004, 02:47 AM   #2
Registered: Sep 2004
Location: Hagerstown, MD
Distribution: Ubuntu Natty Narwahl
Posts: 258

Original Poster
Rep: Reputation: 31
Never mind. I figured it out. In case anyone is curious, here's how I did it:
for i in $(mpc --format ""); do
if [$a = "[playing]"; then
    mpc pause
    mpc play
Old 02-15-2013, 03:08 AM   #3
LQ Newbie
Registered: Jul 2009
Location: Singapore
Distribution: Gentoo, Kubuntu
Posts: 4

Rep: Reputation: 0
mpc has a toggle command (at least since 0.21).

Here's the (overkill) script I made to use toggle:


if ($# > 0) then
        set SERVER="${1}"
        set SERVER=localhost

set MPC=(mpc --host="${SERVER}")

${MPC} version >& /dev/null

if ($? != 0) then
        echo "Can't find mpd server at ${SERVER}..."

        exit 1

${MPC} toggle

# End of script #
Old 02-15-2013, 03:44 AM   #4
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 705

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425

I use the following script to handle media keys on my laptop and bluetooth headphones via DBus:
#!/usr/bin/env python

import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import os

mpc = '' # 'mpc' or '' from
def KeyHandler(x,y):
	def run(cmd):
		return lambda: os.system(mpc + ' ' + cmd)
	'Play': run('toggle'),
	'Stop': run('stop'),
	'Next': run('next'),
	'Previous': run('prev')	}[y]()


	bus = dbus.SessionBus()
	bus_object = bus.get_object('org.gnome.SettingsDaemon',
	bus_object.GrabMediaPlayerKeys("MediaKeys", 0,

	bus_object.connect_to_signal('MediaPlayerKeyPressed', KeyHandler)
	print "Could not bind to Gnome for Media Keys"

loop = gobject.MainLoop()
Make sure this script and (if used) are somewhere in your $PATH and are executable (e.g. chmod +x shows track name/artist/current status(play,pause,stop) in popup notifications.

Last edited by firstfire; 02-16-2013 at 02:18 AM. Reason: Remove unused line from the script.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
your favourite MPD client? killerbob Slackware 3 09-12-2005 02:05 AM
mpd questions. doralsoral Linux - Software 1 06-16-2005 04:57 PM
mpd dependencies doralsoral Linux - Software 3 06-16-2005 05:28 AM
mpd (Music Player) and GAIM sound together. MikeyCarter Linux - Software 3 05-30-2005 01:37 PM
RH & HP4050N PCL - page, pause, page, pause, page andguent Linux - Hardware 0 11-10-2003 09:35 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration