0

我正在制作一个莫尔斯电码程序,所以我需要使用带有 playsound 模块的循环来一次又一次地播放短而长的哔哔声。这些文件在没有循环的情况下正常播放,但是当我添加一个循环时,我遇到了错误。

from playsound import playsound
import time

for i in range(0, 5):
    playsound('long.mp3')
    time.sleep(0.5)
    playsound('short.mp3')
    time.sleep(0.5)
Error 263 for command:
        open long.mp3
    The specified device is not open or is not recognized by MCI.

    Error 263 for command:
        close long.mp3
    The specified device is not open or is not recognized by MCI.
Failed to close the file: long.mp3
Traceback (most recent call last):
  File "C:\Users\achin\PycharmProjects\Morse code\ok.py", line 5, in <module>
    playsound('long.mp3')
  File "C:\Users\achin\PycharmProjects\Morse code\venv\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))
  File "C:\Users\achin\PycharmProjects\Morse code\venv\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException: 
    Error 263 for command:
        open long.mp3
    The specified device is not open or is not recognized by MCI.
4

1 回答 1

0

您是否尝试过使用 with 语句?它看起来像这样:

from playsound import playsound
import time

with open('long.mp3') as long_beep, open('short.mp3') as short_beep:
  for i in range(0, 5):
    playsound(long_beep)
    time.sleep(0.5)
    playsound(short_beep)
    time.sleep(0.5)

另外,它似乎可能是 playsound 的错误(?),请尝试降级到 playsound 1.2.2(pip install playsound==1.2.2)并再次尝试您的代码

于 2021-12-17T08:55:15.547 回答