0

I am making a morse code program so I need to use loops with playsound module to play the short and long beeps again and again. The files are playing properly without a loop but when I add a loop I run into an error.

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.

1 Answers1

0

have you tried using the with statement? it would look something like this:

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)

dditionally, it seems it may be a bug(?) with playsound, try downgrading to playsound 1.2.2 (pip install playsound==1.2.2) and try your code again

ghylander
  • 136
  • 8
  • I am getting another error this time: - Traceback (most recent call last): File "C:\Users\achin\PycharmProjects\Morse code\ok.py", line 4, in with 'long.mp3' as long_beep, 'short.mp3' as short_beep: AttributeError: _ _enter_ _ – Unknown_ _ Dec 17 '21 at 09:08
  • Try adding the open function, see edited answer – ghylander Dec 17 '21 at 09:19
  • It is still not working. Is there any other module I can try that will work with loops ? – Unknown_ _ Dec 17 '21 at 09:30
  • there's pydub (you will need ffmpeg as well to be able to play mp3) and pyaudio (a bit complex). There is a better way to work with this than using loops, i would say. You could store the beeps and their order as a list like [s, l, l] (where s stands for short beep and l for long beep). Then you could write a function that takes the list as input, constructs a sound file according to the contents, and returns it. Would work with playsound too – ghylander Dec 17 '21 at 09:38