hjheins
Posts: 2
Joined: Sun May 12, 2019 11:42 am

Python rfid media player script woes

Sun May 12, 2019 12:02 pm

Hi,

I'm trying to make a raspberry based musicplayer, that starts playing music based on rfid tags that you present to it.
So far, all the parts work, and I have a python script that can read the cards, a config file that links the rfid numbers to a song, and a python script that handles 2 volume buttons (up and down, with down held as mute).
However my issue is actually playing a song when presenting a card. I found a python script for this based on mplayer. However mplayer actually does not support the features used in this script anymore. Hence I tried to modify this to vlc based.
So far, I have little success. The python script recognizes the rfid card, links it to the file, but I can not get it to play on vlc
The project I used as a base is this
The specific scrip is this: rfidmplayer.py

and coding-wise I am here:

Code: Select all

#!/usr/bin/python
import MFRC522
import signal
from configparser import ConfigParser
import os
import vlc
import subprocess 

# Read config file
Config = ConfigParser()
Config.read("/home/pi/MFRC522-python/rfidconfig.txt")

# From https://wiki.python.org/moin/ConfigParserExamples
def ConfigSectionMap(section):
  dict1 = {}
  options = Config.options(section)
  for option in options:
    try:
        dict1[option] = Config.get(section, option)
        if dict1[option] == -1:
            DebugPrint("skip: %s" % option)
    except:
        print("exception on %s!" % option)
        dict1[option] = None
  return dict1

# Send action to vlcplayer
def TagToVlcPlayer (strTag):
  try:
    # Read tag attributes from config 
    strActionType = ConfigSectionMap(strTag)['actiontype']
    strFileUrlFunction = ConfigSectionMap(strTag)['fileurlfunction']
    strDescription = ConfigSectionMap(strTag)['description']

    if strActionType == "URL" or strActionType == "File":
        strAction = strFileUrlFunction
        # Show what will be played
        print ("Audio " + strDescription + " from " + strFileUrlFunction)

    elif strActionType == "Playlist":
        strAction = strFileUrlFunction
	# Show what will be played
        print ("Audio " + strDescription + " from " + strFileUrlFunction)

    elif strActionType == "Function":
        strAction = strFileUrlFunction
 	# Show what action will be performed
        print ("Function " + strFileUrlFunction)

    elif strActionType == "OS":
        strAction = ""
	# Execute on OS, be careful, running as ROOT
        os.system (strFileUrlFunction)

    else:
        strAction = ""

	# Write to cfile
#        with open(cfile, "w") as myfile:
#	 myfile.write(strAction+"\n")
  
  
  except:
        pass

continue_reading = True
MIFAREReader = MFRC522.MFRC522()

# Control file used by mplayer
#cfile = "/tmp/mplayer-control"
instance = vlc.Instance('--input-repeat=-1')
player=instance.media_player_new()
media=instance.media_new(strAction)
player.set_media(media)
player.play()

def end_read(signal, frame):
  global continue_reading
  continue_reading = False
  print ("Ctrl+C captured, ending read.")
  MIFAREReader.GPIO_CLEEN()

signal.signal(signal.SIGINT, end_read)

# Never stop reading
while continue_reading:
  (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  if status == MIFAREReader.MI_OK:
    print ("Card detected")
  (status,backData) = MIFAREReader.MFRC522_Anticoll()
  if status == MIFAREReader.MI_OK:
    strbackData = str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str(backData[4])

    print ("Card read UID: " + strbackData)

    # Do something (based on card and config file)
    TagToVlcPlayer(strbackData)

The issue is that I can not seem to figure out how to send the actual file as imput for playback to vlc.
Can anybody please help?

thank you.
Hendrik-Jan

User avatar
MrYsLab
Posts: 331
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python rfid media player script woes

Sun May 12, 2019 2:57 pm

There is a package called python-vlc that you can use. https://pypi.org/project/python-vlc/

I just ran this script to play an .mp3 and it works for me

Code: Select all

import vlc
import time

player = vlc.MediaPlayer("/home/Music/01. All Souls Night.mp3")
player.play()
while True:
    time.sleep(.1)
The music plays fine, but I do get the following "errors" in the console:

Code: Select all

[01814618] vlcpulse audio output error: PulseAudio server connection failure: Connection refused
[0181ddb8] vlcpulse audio output error: PulseAudio server connection failure: Connection refused
I do not use audio on my RPi. Perhaps someone with more audio experience on the Pi than I can provide a solution to prevent seeing those errors.

BTW, running on Ubuntu on my x86 PC, no errors are shown.

hjheins
Posts: 2
Joined: Sun May 12, 2019 11:42 am

Re: Python rfid media player script woes

Tue May 14, 2019 5:24 pm

Hi,

I am trying to use this, but I am not sure where I should make the code changes...

you get the error because you are probably running this as root.
It tells you that as a sound system you are not using pulse, but likely alsa.

Hendrik-Jan

Return to “Python”