播放WAV歌曲时不跳舞的机器人

2024-09-28 21:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个项目,需要一组涂鸦2舞蹈开始播放一个wav文件,并停止在文件的结尾。你知道吗

(这不是完整的代码,而是我正在测试如何做,以便将其应用于更大的代码。)

from Myro import *
from winsound import*
from time import *

def playSong():
    s=PlaySound('C:\Python34\cantHoldUs.wav',SND_FILENAME)
    sleep(30)
    s.PlaySound(None,SND_FILENAME)

while playSong()==True:
    motors(-1,1)   

歌曲播放并结束,但机器人不动。有人能告诉我怎么做吗?你知道吗


Tags: 文件项目代码fromimport结尾filenamewav
1条回答
网友
1楼 · 发布于 2024-09-28 21:38:32

我建议您将代码重新构造为带有While循环的代码,因为这样更干净、更易于控制:

from time import *

# Play the song
s=PlaySound('C:\Python34\cantHoldUs.wav',SND_FILENAME)

# Start the timer so we can identify when to stop
starttime = time()

# Use a while loop with a True statement until we decide to break it
while True:
    # Make that robot dance!
    motors(-1,1)

    # Check the current time
    stop_time = ((time() - starttime))

    # Stop when 30 seconds is hit
    if stop_time > 30:
        s.PlaySound(None,SND_FILENAME)
        break

    sleep(1)

相关问题 更多 >