The problem is not controlling the MPD by using my script.
It’s starting playback from Volumino web Interface. No reaction to the Play button. Maybe a Timing Problem?
Pressing the Play button first and then starting my script is ok.
Puuting a lot of time.sleep to the script helps some times, but makes my program slow and I think it is not the right way.
Attached you can find my script
#!/home/pi/script python
# Import Modules
#from mpd import MPDClient
import mpd
import RPi.GPIO as GPIO
import struct, time, sys
import os
from os.path import basename
import pygame
from pygame.locals import *
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
DBLUE = ( 0, 0, 16)
# Use GPIO-numbering not PIN numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(8, GPIO.IN)
SYSSTOP = False
# falling edge of key switch
# generates interrupt
def Int_shutdown(channel):
# Stop MPD
SYSSTOP = True
os.system('sudo mpd stop')
print('stopping MPD')
time.sleep(0.1)
# shutdown bei Interrupt
pygame.quit()
print('shutdown now')
GPIO.cleanup()
os.system('sudo shutdown -h now')
# Set GPIO8 as interrupt - callback
GPIO.add_event_detect(8, GPIO.FALLING, callback=Int_shutdown, bouncetime = 2000)
# MPD const and routines
MPD_HOST = 'localhost'
MPD_PORT = '6600'
MPD_PASSWORD = 'volumio' # password for Volumio / MPD
client = mpd.MPDClient()
def mpd_connect():
# Connect with MPD
try:
client.connect(MPD_HOST, MPD_PORT)
return True
except Exception as e:
print repr(e)
return False
def mpd_disconnect():
# Disconnect from MPD
try:
client.close()
client.disconnect()
except Exception as e:
print repr(e)
# prepare Pygames
pygame.init()
pygame.key.set_repeat(100, 100)
screen = pygame.display.set_mode((1024, 600), 0, 32)
pygame.display.set_caption('VOLUMIO+')
pygame.mouse.set_visible(0)
actcolor = BLACK
screen.fill(actcolor)
pygame.display.flip()
counter = 0
# Get new song and display cover art
def Get_NewSong():
if mpd_connect():
currentSong = client.currentsong()
try:
filename = currentSong['file']
except:
filename = ''
try:
album = currentSong['album']
except:
album = ''
try:
composer = currentSong['composer']
except:
composer = ''
try:
artist = currentSong['artist']
except:
artist = ''
try:
track = currentSong['track']
except:
track = ''
try:
title = currentSong['title']
except:
title = ''
try:
ttime = currentSong['time']
except:
ttime = ''
try:
genre = currentSong['genre']
except:
genre = ''
try:
albumartist = currentSong['albumartist']
except:
albumartist = ''
print filename
print album # 3
print composer
print artist # 1
print track
print title # 2
print ttime
print genre
print albumartist
path = os.path.dirname(filename)
file = os.path.splitext(os.path.basename(filename))[0]
print
print file
if title == '':
title = file
print 'Title: '+title
if album == '':
album = os.path.split(path)[1]
print 'Album: '+album
print
if os.path.ismount(path):
print 'gemaountet'
if os.path.isabs(path):
print 'absolute Path'
else:
print 'Path not absolute'
path = os.path.join('/mnt', path)
print 'Available covers:'
picname = ''
covname = ''
stop = False
for tmpname in os.listdir(path):
if stop: break
else:
for extension in ['.jpg', '.bmp', '.png']:
if os.path.splitext(os.path.basename(tmpname))[1] == extension:
print os.path.splitext(os.path.basename(tmpname))[0]
print file
print tmpname.find(file)
covname = tmpname
if (tmpname.find('Cover') > -1) or (tmpname.find('cover') > -1):
picname = tmpname
# Suche abbrechen, wenn ein Cover mit dem Titelnamen gefunden wurde
if tmpname.find(file) > -1:
picname = tmpname
stop = True
# if no cover file is found
if picname == '':
if covname == '':
coverfile = '/home/pi/script/default.jpg'
else:
coverfile = os.path.join(path, covname)
else:
coverfile = os.path.join(path, picname)
print coverfile
coverbitmap = pygame.image.load(coverfile)
size = coverbitmap.get_rect().size
stretch_x = 1024.0 / size[0]
stretch_y = 600.0 / size[1]
if stretch_x > stretch_y:
stretch = stretch_y
else:
stretch = stretch_x
covernew = pygame.transform.scale(coverbitmap, (int(size[0]*stretch), int(size[1]*stretch)))
actcolor = BLACK
screen.fill(actcolor)
myfont = pygame.font.SysFont('Arial.ttf', 24)
textsurf = myfont.render(artist, 1, WHITE)
screen.blit(textsurf, (int(size[0]*stretch), 100))
textsurf = myfont.render(title, 1, WHITE)
screen.blit(textsurf, (int(size[0]*stretch), 200))
textsurf = myfont.render(album, 1, WHITE)
screen.blit(textsurf, (int(size[0]*stretch), 300))
screen.blit(covernew, (0, 0))
pygame.display.update()
mpd_disconnect()
return currentSong
else:
print 'Could not connect to mpd server'
# Read keys from Hama MCE
def Get_Key():
sstop = False
status = client.status
global vol
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
sstop = True # Flag that we are done so we exit this loop
elif event.type == KEYDOWN:
if event.key == 308: # close
sstop = True
elif event.key == 13: # ok or start
# if (event.mod == 256):
if mpd_connect():
client.pause()
print 'play'
mpd_disconnect()
# else:
# if mpd_connect():
# client.pause()
# print 'pause'
# mpd_disconnect()
elif (event.key == K_UP) or (event.key == 280): # up or ch+
if mpd_connect():
client.next()
mpd_disconnect()
elif (event.key == K_DOWN) or (event.key == 281): # down or ch-
if mpd_connect():
client.previous()
mpd_disconnect()
elif event.scancode == 115: # vol+
if vol < 100:
vol = vol+1
print vol
if mpd_connect():
client.setvol(int(vol))
mpd_disconnect()
elif event.scancode == 114: # vol-
if vol > 0:
vol = vol-1
print vol
if mpd_connect():
client.setvol(int(vol))
mpd_disconnect()
elif event.scancode == 113: # mute
if mpd_connect():
status = client.status()
try:
currvol = int(status['volume'])
if (currvol == 0) and (vol > 0):
client.setvol(vol)
else:
client.setvol(0)
except:
print 'volume not available'
mpd_disconnect()
elif event.key == K_RIGHT:
if mpd_connect():
client.seekcur('+5')
mpd_disconnect()
elif event.key == K_LEFT:
if mpd_connect():
client.seekcur(-5)
mpd_disconnect()
elif event.key == 306: # red key for shutdown
Int_shutdown(8)
print event
print event.key
print pygame.key.name(event.key)
return sstop
# MAIN
try:
actualSong = Get_NewSong()
if mpd_connect():
status = client.status()
try:
vol = int(status['volume'])
except:
vol = 50
print vol
mpd_disconnect()
# MAINLOOP
while not SYSSTOP:
if mpd_connect():
if actualSong == client.currentsong():
mpd_disconnect()
time.sleep(3)
else:
mpd_disconnect()
actualSong = Get_NewSong()
SYSSTOP = Get_Key()
time.sleep(3)
except KeyboardInterrupt:
SYSSTOP = True
pygame.quit()
GPIO.cleanup()
sys.exit()
Best regards,
Helgo