Python script to play and stop playing sound on raspberry

Hi Community,

I have a Raspberry Pi B and need a little python script which can play sound when a button at GPIO23 is pressed and stop playing the sound when the button is pressed again. I already have a hardware button/switch connected at GPIO23 and when I press the button it plays a sound file.

The following is the Python script which I'm using right now to just play the sound file. The problem is, that this script is just playing the file once. I need the script to be modifiend to play the sound file endless in a loop (again and again). When the button at GPIO 23 is pressed again the playing of the file should stop.

#!/usr/bin/env python

import [url removed, login to view] as GPIO

from time import sleep

import os

#Setup GPIO and corresponding PIN (23)

[url removed, login to view]([url removed, login to view])

[url removed, login to view](23,[url removed, login to view])

#initialise a previous input variable to 0 (assume button not pressed last)

prev_input = 1

while True:

#Take a reading from GPIO Pin 23

input = [url removed, login to view](23)

#if the last reading was low and this one high, play sound file

if ((not prev_input) and input):

[url removed, login to view]('mpg321 /media/usbstick/[url removed, login to view] &')

#update previous input

prev_input = input

#slight pause to debounce


I got the basics for this file from the following source: [url removed, login to view]

As this is quite a small change I was thinking about to spend 50$ at the maximum.

Kind Regards


Evner: PHP, Software Arkitektur

Se mere: sounds true, python button, one media true, rpi, raspberry, python script, python raspberry, python mp3, playing, GPIO, adafruit, small php script variable, raspberry python php, raspberry python gpio, gpio raspberry, little python, using file python, raspberry php, media play, raspberry system, raspberry python, python setup script, raspberry php python, php python raspberry, hardware php script

Om arbejdsgiveren:
( 1 bedømmelse ) Switzerland

Projekt ID: #5994337

Tildelt til:


Hey, your script is fine so far. All you need is a bool called "playSound=False". When you then press the button, you set this to True and let the while loop run as long as playSound is True. Inside the while loo Flere

$55 USD in 3 dage
(4 bedømmelser)

2 freelancers are bidding on average $139 for this job


Dear Hiring Manager, When You Assign the Project than we will start. My Level Of English is Very Good. I have expertise on Php And wordpress. I have more than 3+ year of experience with Php, wordpress. I can d Flere

$222 USD in 3 dage
(0 bedømmelser)