<p><strong>虽然返回“0”(成功),但没有声音。我的是Python3.7和Win10。我也试过utf-8,utf-16-le,但都没用。</strong></p>
<blockquote>
<p>You need to add <code>wait</code> 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.</p>
</blockquote>
<p>整个代码(ASCII):</p>
<pre><code>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()))
</code></pre>
<p>UNICODE码:</p>
^{pr2}$
<p>如果代码有效,它将返回<code>0: The specified command was carried out</code>。在</p>
<p><strong>注意:</strong></p>
<ol>
<li><a href="https://docs.python.org/3/library/sys.html#sys.getfilesystemencoding" rel="nofollow noreferrer">sys.getfilesystemencoding()</a></li>
</ol>
<blockquote>
<p>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.</p>
<pre><code>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.
</code></pre>
</blockquote>
<ol start=“2”>
<li><a href="https://docs.microsoft.com/en-us/windows/win32/multimedia/opening-a-device#using-an-alias" rel="nofollow noreferrer">Using an Alias</a></li>
</ol>
<blockquote>
<p>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.</p>
</blockquote>
<ol start=“3”>
<li><a href="https://stackoverflow.com/a/56833431/11128312">Avoid using wait</a></li>
</ol>
<blockquote>
<p>If you want play no wait, you need to handle the <code>MCI_NOTIFY</code>, set
the callback window handle, and handle the <code>MM_MCINOTIFY</code> when the
play has finish.</p>
<p><a href="https://docs.microsoft.com/en-us/previous-versions/dd757161(v%3Dvs.85)#parameters" rel="nofollow noreferrer">hwndCallback</a>: Handle to a callback window if the "notify" flag
was specified in the command string.</p>
</blockquote>