简体   繁体   中英

mp3 music loop in python for raspberry pi

i'm new to python and this forum, but i've a little problem that someone may be able to help with? i'm creating a scene for a charity event, where I want my pi to control pneumatic rams, music and other electrical items, like motors.

I have music playing which will be interrupted by the operation of a switch/button, and that will start the sequence of events with the electronics and mechanics, the problem I have is there is an mp3 file playing at the start whilst waiting for a GPIO input which interrupts the mp3, that's fine unless there isn't an input in which case when the mp3 file finishes it just sits there waiting for the GPIO input.

I would like it to loop the mp3 file so that its not just waiting in silence, any help would be appreciated? the file is the first mp3 file, named "Noddy"

try:
    count=10
    while (count>0):
    print 'the count is:', count

os.system('mpg123 -q Make\ Way\ for\ Noddy.mp3 &')
GPIO.wait_for_edge(21, GPIO.FALLING)
subprocess.call(['killall', 'mpg123'])
print "\nFalling edge detected. Now your program can continue with"
print "whatever was waiting for a button press."
# gpio 20 to open and close car doors while lightshow is running
#     GPIO.output(20, 0)
red_led.blink(5, 5, 5)
subprocess.call("sudo python synchronized_lights.py --
file=/home/pi/lightshowpi/music/sample/Happy\ Birthday\ Emma.mp3",         
shell=True)
# gpio 20 to stop open and close of car doors
#     GPIO.output(20, 1)

os.system('mpg123 -q Make\ Way\ for\ Noddy.mp3 &')
GPIO.wait_for_edge(21, GPIO.FALLING)
subprocess.call(['killall', 'mpg123'])
print "\nFalling edge detected. Now your program can continue with"
print "whatever was waiting for a button press."
# gpio 20 to open and close car doors while lightshow is running
GPIO.output(20, 0)
red_led.blink(5, 5, 5)
subprocess.call("sudo python synchronized_lights.py --
file=/home/pi/lightshowpi/music/sample/Happy\ Birthday\ Lucy.mp3",        
shell=True)
# gpio 20 to stop open and close of car doors
GPIO.output(20, 1)
count= count-1
except KeyboardInterrupt:
GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

You can easily play music in loops with PyGame, which is pre-installed in the Pi I believe:

import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play(-1) # note -1 for playing in loops
# do whatever
# when ready to stop do:
pygame.mixer.pause()

If you want to unpause:

pygame.mixer.unpause()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM