我是python和这个论坛的新手,但我有一个小问题,也许有人能帮上忙?我正在为一个慈善活动创建一个场景,在那里我想让我的pi控制气动公羊、音乐和其他电子设备,比如马达。
我有音乐播放,这将被一个开关/按钮的操作中断,这将启动电子和机械的事件序列,我的问题是有一个mp3文件在开始时播放,同时等待GPIO输入中断mp3,这是很好的,除非没有输入,在这种情况下,当mp3文件完成它只是坐在那里等待GPIO输入。
我想让它循环mp3文件,这样它就不会只是静静地等待,任何帮助都将不胜感激?该文件是第一个mp3文件,名为"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
发布于 2018-08-20 05:46:25
你可以使用PyGame轻松循环播放音乐,它预装在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()
如果您想取消暂停:
pygame.mixer.unpause()
https://stackoverflow.com/questions/51921965
复制相似问题