Page 1 of 1

Queuing Audio Output

Posted: Mon Nov 07, 2016 12:26 pm
by tz2623
Hi there,
I am new to python but have some experience programming.
I am trying to output audio using pushbuttons with a small catch.
When two buttons are pressed almost simultaneously we must wait for the first sound to finish before playing the next sound. We are taking the time for each sound to be approximately 3 seconds.
This ideally needs to be extended to more buttons going forward

I have attached where I have got to in the code below, this code specifically has issues as the timer function cannot be used more than once.

Code: Select all

import os
from threading import Timer
from time import sleep
from sys import exit

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
button_1active = False

def sound2():
        print ("playsound2")
        #os.system('mpg123 -q Two.mp3 &')
            #            print("two")


#Timer setup
soundtime = 3
t = Timer(soundtime,sound2)

while True:
        try:
        
                
                if (GPIO.input(23) == False):
                        os.system('mpg123 -q One.mp3 &')                      
                        print("1")
                        button_1active =True
                        
                       
                  
                if (GPIO.input(24) == False):
                        os.system('mpg123 -q Two.mp3 &')
                        print("two")


                if (button_1active == True):
                        if(GPIO.input(24) == False):
                                t.start()
                                
                sleep(0.5);
        
                        
        except KeyboardInterrupt:
                exit()


Re: Queuing Audio Output

Posted: Mon Nov 07, 2016 2:20 pm
by elParaguayo
Well you could use a Queue object and have a thread that handles playing the audio. You could use the subprocess module instead of os.system and then wait for the audio to finish. When the audio finishes, the thread takes the next item from the queue and so on.

This should work with however many buttons you want.

Re: Queuing Audio Output

Posted: Tue Nov 08, 2016 6:19 am
by tz2623
Thank you queuing worked very well.
Wasn't very familiar with subprocessing so used a list for playback still using os.system