首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >树莓派的python中的mp3音乐循环

树莓派的python中的mp3音乐循环
EN

Stack Overflow用户
提问于 2018-08-20 05:25:53
回答 1查看 1.1K关注 0票数 0

我是python和这个论坛的新手,但我有一个小问题,也许有人能帮上忙?我正在为一个慈善活动创建一个场景,在那里我想让我的pi控制气动公羊、音乐和其他电子设备,比如马达。

我有音乐播放,这将被一个开关/按钮的操作中断,这将启动电子和机械的事件序列,我的问题是有一个mp3文件在开始时播放,同时等待GPIO输入中断mp3,这是很好的,除非没有输入,在这种情况下,当mp3文件完成它只是坐在那里等待GPIO输入。

我想让它循环mp3文件,这样它就不会只是静静地等待,任何帮助都将不胜感激?该文件是第一个mp3文件,名为"Noddy“

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-08-20 05:46:25

你可以使用PyGame轻松循环播放音乐,它预装在Pi I believe中:

代码语言:javascript
复制
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()

如果您想取消暂停:

代码语言:javascript
复制
pygame.mixer.unpause()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51921965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档