LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Play/Pause Script for MPD (https://www.linuxquestions.org/questions/programming-9/play-pause-script-for-mpd-268273/)

drj000 12-20-2004 12:17 AM

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.

drj000 12-20-2004 01:47 AM

Never mind. I figured it out. In case anyone is curious, here's how I did it:
Code:

#!/bin/bash
for i in $(mpc --format ""); do
    a=$i
    break
done
if [$a = "[playing]"; then
    mpc pause
else
    mpc play
fi


kwutchak 02-15-2013 02:08 AM

mpc has a toggle command (at least since 0.21).

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

Code:

#!/bin/tcsh

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

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

${MPC} version >& /dev/null

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

        exit 1
endif

${MPC} toggle

# End of script #


firstfire 02-15-2013 02:44 AM

Hi.

I use the following script to handle media keys on my laptop and bluetooth headphones via DBus:
Code:

#!/usr/bin/env python

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

mpc = 'mpd_osd.py' # 'mpc' or 'mpd_osd.py' from http://mpd.wikia.com/wiki/Mpd_osd.py_%28using_dbus%29
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]()

DBusGMainLoop(set_as_default=True)

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

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


loop = gobject.MainLoop()
loop.run()

Make sure this script and mpd_osd.py (if used) are somewhere in your $PATH and are executable (e.g. chmod +x media-keys.py). mpd_osd.py shows track name/artist/current status(play,pause,stop) in popup notifications.


All times are GMT -5. The time now is 05:44 AM.