Last active
October 20, 2018 02:30
-
-
Save MarkKoz/52076e3a7c0aa98ae2a2ce04c26ae8a9 to your computer and use it in GitHub Desktop.
MCI CD Audio Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
from ctypes.wintypes import * | |
winmm = windll.winmm | |
class MciException(Exception): | |
def __init__(self, code): | |
self.code = code | |
success, self.message = mci_get_error_string(code) | |
if success == 0: | |
self.message = 'Unknown error.' | |
def __str__(self): | |
return f'{self.code}: {self.message}' | |
def mci_send_string(lpszCommand: str, hwndCallback=None): | |
mciSendString = winmm.mciSendStringA | |
mciSendString.argtypes = [LPCSTR, LPSTR, UINT, HANDLE] | |
mciSendString.restype = DWORD | |
lpszCommand = LPCSTR(lpszCommand.encode()) | |
lpszReturnString = create_string_buffer(256) | |
cchReturn = UINT(256) | |
hwndCallback = HANDLE(hwndCallback) | |
error_code = mciSendString( | |
lpszCommand, lpszReturnString, cchReturn, hwndCallback) | |
if error_code != 0: | |
raise MciException(error_code) | |
else: | |
return lpszReturnString.value.decode() | |
def mci_get_error_string(fdwError: DWORD): | |
mciGetErrorString = winmm.mciGetErrorStringA | |
mciGetErrorString.argtypes = [DWORD, LPSTR, UINT] | |
mciGetErrorString.restype = BOOL | |
lpszErrorText = create_string_buffer(1024) | |
cchErrorText = UINT(1024) | |
res = mciGetErrorString(fdwError, lpszErrorText, cchErrorText) | |
return (res, lpszErrorText.value.decode()) | |
def mci_open(lpszDevice, lpszOpenFlags='', lpszFlags='wait'): | |
lpszCommand = f'open {lpszDevice} {lpszOpenFlags} {lpszFlags}' | |
return mci_send_string(lpszCommand) | |
def mci_close(lpszDeviceID, lpszFlags='wait'): | |
lpszCommand = f'close {lpszDeviceID} {lpszFlags}' | |
return mci_send_string(lpszCommand) | |
def mci_status(lpszDeviceID, lpszRequest, lpszFlags='wait'): | |
lpszCommand = f'status {lpszDeviceID} {lpszRequest} {lpszFlags}' | |
return mci_send_string(lpszCommand) | |
def mci_capability(lpszDeviceID, lpszRequest, lpszFlags='wait'): | |
lpszCommand = f'capability {lpszDeviceID} {lpszRequest} {lpszFlags}' | |
return mci_send_string(lpszCommand) | |
def main(): | |
open_msg = mci_open('cdaudio') | |
print(open_msg) | |
status = mci_status('cdaudio', 'mode') | |
print(status) | |
capability = mci_capability('cdaudio', 'can eject') | |
print(capability) | |
close_msg = mci_close('cdaudio') | |
print(close_msg) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment