TaylorSMarks / playsound

Pure Python, cross platform, single function module with no dependencies for playing sounds.
Other
517 stars 114 forks source link

The `playsound` library encountered an issue when attempting to use the temporary file path in Windows, resulting in an error during playback. #147

Open marvinbraga opened 1 year ago

marvinbraga commented 1 year ago

Issue Description

Problem

The playsound library encountered an issue when attempting to use the temporary file path in Windows, resulting in an error during playback.

  File "C:\Users\user_name\AppData\Local\pypoetry\Cache\virtualenvs\llms-course-cecv-ynhqYloT-py3.10\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))
  File "C:\Users\user_name\AppData\Local\pypoetry\Cache\virtualenvs\llms-course-cecv-ynhqYloT-py3.10\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException: 
    Error 305 for command:
        open "C:\Users\user_name\AppData\Local\Temp\PSelb4boc8.mp3"

Solution

An adjustment was made to the _playsoundWin function to correctly utilize the temporary file path. The modification involved using os.path.normpath to ensure the correct formatting of the temporary file path in Windows.

Code Modification

# Existing code
    def winCommand(*command):
        bufLen = 600
        buf = c_buffer(bufLen)
        command = ' '.join(command).encode('utf-16')
        errorCode = int(windll.winmm.mciSendStringW(command, buf, bufLen - 1, 0))  # use widestring version of the function
        if errorCode:
            errorBuffer = c_buffer(bufLen)
            windll.winmm.mciGetErrorStringW(errorCode, errorBuffer, bufLen - 1)  # use widestring version of the function
            exceptionMessage = ('\n    Error ' + str(errorCode) + ' for command:'
                                '\n        ' + command.decode('utf-16') +
                                '\n    ' + errorBuffer.raw.decode('utf-16').rstrip('\0'))
            logger.error(exceptionMessage)
            raise PlaysoundException(exceptionMessage)
        return buf.value

    if '\\' in sound:
        sound = '"' + sound + '"'

    # Modified code (added line to normalize the temporary file path)
    sound = os.path.normpath(sound)

This modification ensures that the temporary file path is correctly formatted for Windows, allowing playsound to utilize the temporary file without encountering errors.

Steps to Reproduce

  1. Use the playsound library on Windows.
  2. Attempt to play an audio file using a temporary file path.
  3. Observe the error encountered due to incorrect formatting of the temporary file path.

Expected Behavior

The playsound library should be able to utilize the temporary file path correctly on Windows and play the audio file without errors.