I would like to create a simple audio player with Python which is controlled with one button. When I press the button the first time, the .wav should begin playing, but how can I stop it when the button is pressed again?

I´m using the command line:

os.system("aplay /sound.wav")

I tried it also with pygame, that worked, but when the Pi ran longer then 6 hours it played a teribble noise.


Read up on subprocess.popen... in https://docs.python.org/3/library/subprocess.html?highlight=subprocess.popen#subprocess.Popen

But a simple way using import os is: (I used ping rather than aplay for testing, as it also runs 'forever') the close kills the subprocess.

import os
# do what you need to do to sense the button pushed 
p = os.popen('ping')
# do what you need to do to sense the button pushed 

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.