pi-rfid-jukebox/player.py

59 lines
1.4 KiB
Python

from pirc522 import RFID
from glob import glob
import atexit
import os
import time
import signal
import subprocess
import vlc
rdr = RFID()
util = rdr.util()
last_seen = None
seen_at = 0
p = None
RETRY_TIME = 2
#player = subprocess.Popen(["mpg321", "-R", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
player = subprocess.Popen(["mpg321", "-R", "-"], stdin=subprocess.PIPE)
player.stdin.write("GAIN 100")
try:
while True:
(error, tag_type) = rdr.request()
if not error:
(error, uid) = rdr.anticoll()
if not error:
tag_id = ''.join(hex(x)[2:].zfill(2) for x in uid)
print("Tag: " + tag_id)
if tag_id == 'b9506555d9':
print("Stopping playback.")
player.stdin.write("STOP\n")
continue
if time.time() > (seen_at + RETRY_TIME):
last_seen = uid
seen_at = time.time()
if tag_id == 'aa89665510':
print("Toggling pause.")
player.stdin.write("PAUSE\n")
continue
file = glob("media/" + tag_id + " - *.mp3")
if file:
print("Playing file " + file[0])
player.stdin.write("STOP\n")
player.stdin.write("LOAD " + file[0] + "\n")
continue
except (KeyboardInterrupt, SystemExit):
os.kill(player.pid, signal.SIGTERM)
# Calls GPIO cleanup
rdr.cleanup()