tz2623
Posts: 2
Joined: Mon Nov 07, 2016 12:13 pm

Queuing Audio Output

Mon Nov 07, 2016 12:26 pm

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()


User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Queuing Audio Output

Mon Nov 07, 2016 2:20 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

tz2623
Posts: 2
Joined: Mon Nov 07, 2016 12:13 pm

Re: Queuing Audio Output

Tue Nov 08, 2016 6:19 am

Thank you queuing worked very well.
Wasn't very familiar with subprocessing so used a list for playback still using os.system

Return to “Python”