No sound although it returns "0" (successful). Mine is Python3.7 and Win10. I also tried utf-8, utf-16-le, nothing worked.
You need to add wait
flag. Having this flag on, allows you indeed to
wait until the called function completed. The reason you can actually
play your file. In the case you removed it, it will initiate the play
and immediately after close it.
The Whole code(ASCII):
from ctypes import c_buffer, windll
from sys import getfilesystemencoding
if __name__ == '__main__':
buf = c_buffer(255)
filesystemencoding = getfilesystemencoding()
filename = r'.\file_example.mp3'
# ASCII
command = 'open ' + filename + ' alias test2'
waitcommand = 'play test2 wait'
byte_string_command = command.encode(filesystemencoding)
waiting = waitcommand.encode(filesystemencoding)
errorCode = int(windll.winmm.mciSendStringA(byte_string_command, buf, 254, 0))
# errorCode should be 275: Cannot find the file
errorBuffer = c_buffer(255)
windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
print("{}: {}".format(errorCode, errorBuffer.value.decode()))
errorCode = int(windll.winmm.mciSendStringA(waiting, buf, 254, 0))
# errorCode should be 275: Cannot find the file
errorBuffer = c_buffer(255)
windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
print("{}: {}".format(errorCode, errorBuffer.value.decode()))
UNICODE:
from ctypes import c_buffer, windll
from sys import getfilesystemencoding
if __name__ == '__main__':
buf = c_buffer(255)
filesystemencoding = getfilesystemencoding()
filename = r'.\file_example.mp3'
# ASCII
command = r'open ' + filename + r' alias test2'
waitcommand = r'play test2 wait'
byte_string_command = command.encode(filesystemencoding)
waiting = waitcommand.encode(filesystemencoding)
# Unicode
errorCode = int(windll.winmm.mciSendStringW(command, buf, 254, 0))
# errorCode should be 296: The specified file cannot be played
errorBuffer = c_buffer(255)
windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
print("{}: {}".format(errorCode, errorBuffer.value.decode()))
errorCode = int(windll.winmm.mciSendStringW(waitcommand, buf, 254, 0))
# errorCode should be 275: Cannot find the file
errorBuffer = c_buffer(255)
windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
print("{}: {}".format(errorCode, errorBuffer.value.decode()))
If code works, it will return 0: The specified command was carried out
.
Note:
- sys.getfilesystemencoding()
Return the name of the encoding used to convert between Unicode
filenames and bytes filenames. For best compatibility, str should be
used for filenames in all cases, although representing filenames as
bytes is also supported. Functions accepting or returning filenames
should support either str or bytes and internally convert to the
system’s preferred representation.
This encoding is always ASCII-compatible.
[os.fsencode()][2] and [os.fsdecode()][3] should be used to ensure that the
correct encoding and errors mode are used.
In the UTF-8 mode, the encoding is utf-8 on any platform.
On macOS, the encoding is 'utf-8'.
On Unix, the encoding is the locale encoding.
On Windows, the encoding may be 'utf-8' or 'mbcs', depending on user
configuration.
Changed in version 3.6: Windows is no longer guaranteed to return
'mbcs'. See PEP 529 and [_enablelegacywindowsfsencoding()][4] for more
information.
Changed in version 3.7: Return ‘utf-8’ in the UTF-8 mode.
- Using an Alias
When you open a device, you can use the "alias" flag to specify a
device identifier for the device. This flag lets you assign a short
device identifier for compound devices with lengthy filenames, and
it lets you open multiple instances of the same file or device.
- Avoid using wait
If you want play no wait, you need to handle the MCI_NOTIFY
, set
the callback window handle, and handle the MM_MCINOTIFY
when the
play has finish.
hwndCallback: Handle to a callback window if the "notify" flag
was specified in the command string.