LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-20-2004, 12:17 AM   #1
drj000
Member
 
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, 01:47 AM   #2
drj000
Member
 
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:
Code:
#!/bin/bash
for i in $(mpc --format ""); do
    a=$i
    break
done
if [$a = "[playing]"; then
    mpc pause
else
    mpc play
fi
 
Old 02-15-2013, 02:08 AM   #3
kwutchak
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:

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 #
 
Old 02-15-2013, 02:44 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 615

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
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.

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


Reply


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 01:05 AM
mpd questions. doralsoral Linux - Software 1 06-16-2005 03:57 PM
mpd dependencies doralsoral Linux - Software 3 06-16-2005 04:28 AM
mpd (Music Player) and GAIM sound together. MikeyCarter Linux - Software 3 05-30-2005 12:37 PM
RH & HP4050N PCL - page, pause, page, pause, page andguent Linux - Hardware 0 11-10-2003 08:35 AM


All times are GMT -5. The time now is 10:03 PM.

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