1

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);
3
  • I've got a suspicion it's your quotes. Could you try replacing the single quotes around 'mpg123' in your subprocess.Popen lines with double quotes - "mpg123"?
    – goobering
    Jun 24, 2016 at 14:58
  • thank you honestly I am excited to try that. Also In my real code the directory is ---- for f in listdir('\home\pi') ------. This is where my mp3s are too.
    – user202815
    Jun 24, 2016 at 15:04
  • before adding quotes I noticed "import subprocess" was missing. So now the problem is that when pressing button 1 the entire list of mp3s is printed and you can not cycle through them as you would want to >>?
    – user202815
    Jun 25, 2016 at 1:00

1 Answer 1

-2

The first mistake I see in this code is on the print command.

print "No mp3 files found!"

...should be enclosed in brackets, like this.

print ("No mp3 files found!")

...the next set of print commands too should also look like this, with opening and closing brackets.

print ('--- Available mp3 files ---')

print (mp3_files)

print ('--- Press button #1 to select mp3, button #2 to play current. ---')

Otherwise it throws an error when you come to run it.

There were other problems I found too for the GPIO setup, on the debug output it kept polling through the GPIO instructions, as though it wasn't sure if the pins were high or low, so I made these changes which fixed it all by pulling the initial state of the pins to HIGH.

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
1
  • 4
    The shown print statements are ok for Python 2.x and are certainly not related to the reported problems (i.e. in this case the OP would have said to have gotten a SyntaxError).
    – Ghanima
    Apr 2, 2017 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.