Nov-15-2018, 12:21 PM
Hi,
The following code runs on a Raspberry and plays a song in a loop unless a button is pressed which stops the execution and exits file.
The button works as well the loop however, when the loop ends normally (without the use of the button), the line os._exit(1) is not executed causing the file to remain loaded into the memories.
How to I solve it? Also, how do I pass the variables (vol and file) from an external file?
TIA
The following code runs on a Raspberry and plays a song in a loop unless a button is pressed which stops the execution and exits file.
The button works as well the loop however, when the loop ends normally (without the use of the button), the line os._exit(1) is not executed causing the file to remain loaded into the memories.
How to I solve it? Also, how do I pass the variables (vol and file) from an external file?
TIA
import RPi.GPIO as GPIO import time, signal, os from multiprocessing import Process from subprocess import check_output buttonpin = 23 #GPIO23 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(buttonpin, GPIO.IN, pull_up_down=GPIO.PUD_UP) m_pid = 0 file = './sounds/bleep_01.wav' vol = 5 def play(): '''play music''' for x in range(3): command = 'mplayer -softvol -volume %d %s 1>/dev/null 2>&1' % (vol, file) os.system(command) os._exit(1) def get_pid(): '''gets mplayer's actual pid''' global m_pid m_pid = int(check_output(["pidof","-s","mplayer"])) def kill_pid(pid): '''kills mplayer's pid''' os.kill(pid, signal.SIGKILL) def button(): '''checks if button is pressed''' while True: input_state = GPIO.input(buttonpin) if input_state == False: time.sleep(0.3) #debounce GPIO.cleanup() kill_pid(m_pid) if __name__ == '__main__': p1 = Process(target=play) p1.start() p2 = Process(target=get_pid) p2.start() p3 = Process(target=button) p3.start() p1.join() p2.join() p3.join()