0

I am using a RPi3 in a museum to connect to 2 buttons via GPIO. Each button plays an mp3 file. I used the Adafruit guide here: https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/overview

and everything works as advertised. However, the files are around 30 minutes long, and if the person starts playing one track, gets a few minutes then realizes they want to play the other track, they can't. The first file has to play to the end, and only then can the second button be pushed. So what I want to do if possible is to add a 'stop' button, a third button which, when pressed stops whichever track is playing, and allows the other track to be chosen. Or better still, when either of the 2 buttons is pushed to select a track, it stops whatever track is already playing. Below is the python script I'm using.

#!/usr/bin/env python

import os
from time import sleep

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)

while True:
    if (GPIO.input(23) == False):
        os.system('mpg123 -q audio1.mp3 &')

    if (GPIO.input(24) == False):
        os.system('mpg123 -q audio2.mp3 &')


    sleep(0.5);

Any guidance is much appreciated, thanks.

  • wouldn't it be easier to change the button code to cancel whatever is already playing then play the new track? – Steve Robillard Feb 24 '18 at 21:56
  • so why don't you help others and answer your own question? – Steve Robillard Feb 25 '18 at 2:23
  • #!/usr/bin/env python import os import subprocessfrom time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN) GPIO.setup(24, GPIO.IN) while True: if (GPIO.input(23) == False): os.system('mpg123 -q audio1.mp3 &') if (GPIO.input(24) == False): os.system('mpg123 -q audio2.mp3 &') sleep(0.5); – Nick T Feb 25 '18 at 2:24
  • Sorry Steve, I am really struggling with how to paste my script into this comment window while retaining the formatting. – Nick T Feb 25 '18 at 2:31
  • I've made a 'stop' button work, but now I need to disable both of the buttons which trigger the 2 files while either one of the files is playing. At the moment, if you press one button, then the other, both files start playing at once. if you press one button, wait, then press it again, the same track plays twice, out of time. as kids tend to bang on buttons in museums, I would like to stop this happening. And ideas? – Nick T Feb 25 '18 at 2:31
0

OK, here is the script which worked.

#!/usr/bin/env python


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)

while True:
    if (GPIO.input(23) == False):
        subprocess.Popen(['mpg123', 'audio1.mp3'])
        sleep(1)

    if (GPIO.input(24) == False):
        subprocess.Popen(['mpg123', 'audio2.mp3'])
        sleep(1)

    if (GPIO.input(25) == False):
        subprocess.call(['killall', 'mpg123'])

    sleep(0.5); 

Had to use subprocess instead of os.system, that enabled the process to be killed. I think. Now the problem I have is that I need to disable the 2 trigger buttons for the duration of the audio tracks, otherwise the tracks can be played over the top of each other with multiple button pushes. (Kids.)

I tried to do this by increasing the sleep command which I assumed was in seconds, to be the same length as the audio file, but this didn't work. Is there another way?

  • You need to separate the answer from the new question – Steve Robillard Feb 25 '18 at 2:46
  • So start a new thread? – Nick T Feb 25 '18 at 2:48
  • Yes, I also suggest you take the tour. Please note that basic programming questions (those not specific to the Pi - and use of a Pi does not make it specific to the PI.) Belong on our larger sister site stackoverflow.com – Steve Robillard Feb 25 '18 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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