Having followed the adafruit "overview playing music with buttons raspberry pi" I thought I knew it all but then I wanted to play random files and then to scroll through the files and play them by button press.
couple notes: the mp3 files are in the same \home\pi directory as the python script. What I want is to flow through 4 separate music folders by genre, but don't know why adafruit neglected to address this topic.
The error(s) you ask? 1)"subprocess is not defined" What do I do? 2)"the file could not be found to even play
#!/usr/bin/env python
from os import listdir
import subprocess
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
mp3_files = [ f for f in listdir('.') if f[-4:] == '.mp3' ]
if not (len(mp3_files) > 0):
print "No mp3 files found!"
print '--- Available mp3 files ---'
print mp3_files
print '--- Press button #1 to select mp3, button #2 to play current. ---'
index = 0
while True:
if (GPIO.input(23) == False):
index += 1
if index >= len(mp3_files):
index = 0
print "--- " + mp3_files[index] + " ---"
if (GPIO.input(24) == False):
subprocess.Popen(['mpg123', mp3_files[index]])
print '--- Playing ' + mp3_files[index] + ' ---'
print '--- Press button #3 to clear playing mp3s. ---'
sleep(1)
if (GPIO.input(25) == False):
subprocess.call(['killall', 'mpg123'])
print '--- Cleared all existing mp3s. ---'
sleep(0.1);