From f63e53f5df02b804f78cfe188b83ba7fc48a7a4f Mon Sep 17 00:00:00 2001 From: Tilman Date: Sat, 1 Dec 2018 00:24:39 +0000 Subject: [PATCH] Durchbruch mit replaceAndPlay? --- code/piplayer4.py | 69 ++++++++++------------------------------------- 1 file changed, 14 insertions(+), 55 deletions(-) diff --git a/code/piplayer4.py b/code/piplayer4.py index 210f737..38e6e74 100644 --- a/code/piplayer4.py +++ b/code/piplayer4.py @@ -1,28 +1,22 @@ # piplayer4 - AudioLauncher # http://www.tilman.de/piplayer4 -# TODO -# logging evtl. ausdünnen -# implement seek forward/back - import RPi.GPIO as GPIO import logging import time import subprocess import select # for polling zbarcam, see http://stackoverflow.com/a/10759061/3761783 -from socketIO_client import SocketIO, LoggingNamespace # https://gist.github.com/ivesdebruycker/4b08bdd5415609ce95e597c1d28e9b9e +from socketIO_client import SocketIO, LoggingNamespace # see https://gist.github.com/ivesdebruycker/4b08bdd5415609ce95e597c1d28e9b9e from threading import Thread -logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s - %(message)s') -logging.info('Initializing TEST') +logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s') +logging.info('Initializing') # Configuration MUSIC_BASE_DIRECTORY = "mnt/NAS/" SOUND_SCANNING = "mnt/INTERNAL/sounds/scanning.mp3" -SOUND_OK = "mnt/INTERNAL/sounds/ok-05.mp3" SOUND_SCAN_FAIL = "mnt/INTERNAL/sounds/fail-05.mp3" -SOUND_PLAYBACK_ERROR = "mnt/INTERNAL/sounds/error.mp3" QR_SCANNER_TIMEOUT = 4 @@ -52,18 +46,13 @@ GPIO.setup(PIN_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(PIN_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP) -pending_play = False # to determine whether we should send a play command after we received a pushState event is_playing = False # for toggling play/pause (stores the current status from the pushState events) -socketIO = SocketIO('127.0.0.1', 3000) +socketIO = SocketIO('localhost', 3000) -def play_sound(filename): - socketIO.emit('clearQueue') - logging.info('Cleared queue, now playing ' + filename) - global pending_play - pending_play = True - socketIO.emit('addToQueue', {'service':'mpd','uri':filename}) +def play(uri, service = 'mpd'): + socketIO.emit('replaceAndPlay', {'service':service,'uri':uri}) def prev_callback(channel): logging.info("PREV") @@ -75,11 +64,9 @@ def play_callback(channel): if is_playing: logging.info("PAUSE") socketIO.emit('pause') - is_playing = False else: logging.info("PLAY") socketIO.emit('play') - is_playing = True def next_callback(channel): logging.info("NEXT") @@ -88,30 +75,13 @@ def next_callback(channel): def on_pushState(*args): logging.debug(args[0]['status']) - global is_playing, pending_play + global is_playing if args[0]['status'] == 'play': is_playing = True - pending_play = False else: is_playing = False - if pending_play: - logging.info("start pending play --------------") - socketIO.emit('play') -def on_pushQueue(*args): - return -# logging.info("pushQueue") -# logging.info(args[0]) -# global pending_play -# logging.info(pending_play) -# if pending_play: -## if not args[0]: -## play_sound(SOUND_PLAYBACK_ERROR) -## else: -# socketIO.emit('play', '1') -# pending_play = False - -def queue_events_thread(): +def events_thread(): socketIO.wait() @@ -121,9 +91,8 @@ GPIO.add_event_detect(PIN_NEXT, GPIO.FALLING, callback=next_callback, bouncetime try: - socketIO.on('pushQueue', on_pushQueue) socketIO.on('pushState', on_pushState) - listener_thread = Thread(target=queue_events_thread) + listener_thread = Thread(target=events_thread) listener_thread.daemon = True listener_thread.start() @@ -132,7 +101,7 @@ try: GPIO.wait_for_edge(PIN_SENSOR, GPIO.FALLING) # TEST logging.info('Photo sensor active, activating light and camera') - #play_sound(SOUND_SCANNING) + play(SOUND_SCANNING) # turn LED on GPIO.output(PIN_LED, GPIO.HIGH) @@ -150,35 +119,25 @@ try: poll_result = poll_obj.poll(100) if (poll_result): - - # play confirmation sound - #play_sound(SOUND_OK) - #socketIO.emit('removeFromQueue', '0') - qr_code = zbarcam.stdout.readline().rstrip() qr_code = qr_code.decode("utf-8") # python3 logging.info("QR Code: " + qr_code) - socketIO.emit('clearQueue') - if qr_code.startswith("http://"): - socketIO.emit('addToQueue', {'service':'webradio','uri':qr_code}) + play(qr_code, 'webradio') elif qr_code.startswith("spotify:"): - #socketIO.emit('addToQueue', {'service':'spop','uri':qr_code}) - socketIO.emit('addToQueue', {'uri':'mnt/NAS/Audio/Singles/Blazin_Squad_-_Flip_Reverse.mp3'}) + play(qr_code, 'spop') else: # create full path if (qr_code.startswith("/")): qr_code = qr_code[1:] full_path = MUSIC_BASE_DIRECTORY + qr_code logging.info("full_path: " + full_path) - socketIO.emit('addToQueue', {'uri':full_path}) - - pending_play = True + play(full_path) else: logging.warning('Timeout on zbarcam') - #play_sound(SOUND_SCAN_FAIL) + play(SOUND_SCAN_FAIL) zbarcam.terminate() GPIO.output(PIN_LED, GPIO.LOW)