How to play the same sound every minute?

62 views Asked by At

I'm trying to play the same sound every minute using the playsound library. In the first lap it plays the sound, but the second time it ends with an error.

The code:

from playsound import playsound
from datetime import datetime
import time

while 1:
    playsound('mysound.mp3')
    time.sleep(60 - datetime.now().second)

The error:

    Error 263 for command:
        open mysound.mp3
    The specified device is not open or is not recognized by MCI.

    Error 263 for command:
        close mysound.mp3
    The specified device is not open or is not recognized by MCI.
Failed to close the file: mysound.mp3
---------------------------------------------------------------------------
PlaysoundException                        Traceback (most recent call last)
c:\Users\ ...\notebook.ipynb Cell 4 line 8
     
      5 import time
      7 while 1:
----> 8     playsound('mysound.mp3')
      9     time.sleep(60 - datetime.now().second)
     12 #playsound('mysound.mp3')
     13 
     14 #def get_data(): 
   (...)
     19 #      
     20 #persistencia(get_data)

File c:\src\miniconda\Lib\site-packages\playsound.py:72, in _playsoundWin(sound, block)
     
     70 try:
     71     logger.debug('Starting')
---> 72     winCommand(u'open {}'.format(sound))
     73     winCommand(u'play {}{}'.format(sound, ' wait' if block else ''))
     74     logger.debug('Returning')

File c:\src\miniconda\Lib\site-packages\playsound.py:64, in _playsoundWin.<locals>.winCommand(*command)
     
     60     exceptionMessage = ('\n    Error ' + str(errorCode) + ' for command:'
     61                         '\n        ' + command.decode('utf-16') +
     62                         '\n    ' + errorBuffer.raw.decode('utf-16').rstrip('\0'))

...

PlaysoundException: 
    Error 263 for command:
        open mysound.mp3
    The specified device is not open or is not recognized by MCI.
1

There are 1 answers

0
Howsikan On BEST ANSWER

This solution, adopted from Simas Joneliunas's solution here worked for me. It is likely that you need to pass a full file path to playsound(). The source code for this module may give you more insight into how it looks for files to play. I use a Windows computer so my path to the audio file needs the double slashes \\. This is not necessary for Linux or MacOS systems.

from playsound import playsound
from datetime import datetime
import time, os

while 1:
    playsound(os.path.dirname(__file__) + '\\sound_77491782.mp3')
    time.sleep(60 - datetime.now().second)