我正在尝试使用.mp3 ( winmm.dll,ctypes.windll.winmm)在python中播放一个ctypes.windll.winmm文件。但是当我试图以毫秒为单位获取某个文件的长度时,而不是实际长度(05:23 =大约323000毫秒),我只得到了3。状态命令得到的时间格式是"m",在set命令之后它不会改变。下面是一些说明问题的代码:
from ctypes import windll, c_buffer
fp = 'song.mp3'
alias = 'test'
buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'open "{fp}" alias {alias}', buf, 254, 0)
print(r)
buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'status {alias} time format', buf, 254, 0)
print(r, buf.value)
buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'set {alias} time format milliseconds', buf, 254, 0)
print(r)
buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'status {alias} time format', buf, 254, 0)
print(r, buf.value)
buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'status {alias} length', buf, 254, 0)
print(r, buf.value)及其产出:
0
0 b'm'
0
0 b'm'
0 b'3'提前感谢您的帮助!
发布于 2019-12-15 18:37:19
正如我在评论中所指出的,这是一个典型的未定义行为示例。有关更多细节,请查看[SO]:通过ctype从Python调用的C函数返回不正确的值(@CristiFati的答案)。
另外,你还混合了8位和16位的字符串。
清单[Python3.Docs]:ctypes Python的外部函数库。
code00.py
#!/usr/bin/env python3
import sys
import ctypes as ct
from ctypes import wintypes as wt
def main(*argv):
winmm_dll = ct.WinDLL("Winmm.dll")
mciSendString = winmm_dll.mciSendStringW
mciSendString.argtypes = (wt.LPCWSTR, wt.LPWSTR, wt.UINT, wt.HANDLE)
mciSendString.restype = wt.DWORD
file_name = "./SampleAudio_0.4mb.mp3" # Downloaded from https://www.sample-videos.com/audio/mp3/crowd-cheering.mp3
alias_name = "test00"
status_commands = [
f"status {alias_name} time format",
f"status {alias_name} length",
]
commands = [f"open \"{file_name}\" alias {alias_name}"]
commands.extend(status_commands)
commands.append(f"set {alias_name} time format ms")
commands.extend(status_commands)
commands.append(f"close {alias_name}")
out_buf_len = 0xFF
out_buf = ct.create_unicode_buffer(out_buf_len)
for command in commands:
print(f"Sending {command}...")
res = mciSendString(command, out_buf, out_buf_len, None)
print(f" Result: {res}\n Output: {out_buf.value}")
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main(*sys.argv[1:])
print("\nDone.")输出
cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q059343461> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe“编码00.py Python3.7.3 (v3.7.3:ef4ec6ed12,2019年3月25日,22:22:05) MSC v.1916 64位(AMD64) 64位(AMD64)64位( win32发送打开)。结果:0输出:1发送状态test00时间格式。结果:0输出:毫秒发送状态test00长度结果:0输出: 27746发送集test00时间格式ms。结果:0输出:发送状态test00时间格式结果:0输出:毫秒发送状态test00长度结果:0输出: 27746发送关闭test00.结果:0输出:完成。
https://stackoverflow.com/questions/59343461
复制相似问题