0

I am using this python script to use 2 buttons to trigger 2 different mp3 files in a museum exhibit. As the files are 30 minutes long, I have also added a stop button to stop the playing mp3 so that the user doesn't have to listen to the end before listening to the other track. This is all working perfectly.

But I now 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?

I am also open to the idea of a 2 button system where each button press kills whatever mp3 is playing, then plays the selected mp3.

#!/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); 

closed as off-topic by Milliways, techraf, Steve Robillard, Aurora0001, Jacobm001 Feb 26 '18 at 3:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Milliways, techraf, Steve Robillard, Aurora0001, Jacobm001
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you tried moving the subprocess kill before the two calls that play the mp3? You may need a short sleep in between. – Steve Robillard Feb 25 '18 at 3:21
  • This is not a Pi question - it is general programming. Just make any button press kill whatever is playing before starting new. – Milliways Feb 25 '18 at 3:32
  • @Milliways thank you, I realise that now. My apologies for posting in the wrong place, and thank you for letting me know. – Nick T Feb 25 '18 at 4:16
  • @Steve Robillard Thank you so much, this did the trick. Seems obvious now that you have suggested it. I very much appreciate both your help and your patience. – Nick T Feb 25 '18 at 4:17
2

OK here's the working script, big thanks to Steve Robillard.

#!/usr/bin/env python

import os
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.call(['killall', 'mpg123'])
        sleep(0.1)
        subprocess.Popen(['mpg123', 'audio1.mp3'])

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


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

The killall subprocess in each of the two buttons makes the 3rd killall button (GPIO 25) redundant as well, which is nice, simplifies everything.

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