开发者

How to play high quality music (320kpbs) by using mciSendString

开发者 https://www.devze.com 2023-02-05 17:24 出处:网络
I try to play high quality music (320kpbs) by using mciSendString, but it doesn\'t work. And the mciSendString return value 开发者_运维问答is MCIERR_INVALID_DEVICE_NAME (263). How to solve it?I\'ve us

I try to play high quality music (320kpbs) by using mciSendString, but it doesn't work. And the mciSendString return value 开发者_运维问答is MCIERR_INVALID_DEVICE_NAME (263). How to solve it?


I've used mciSendString before to render an mpeg video on the window of whatever application you currently had open. Playing an audio file would be almost identical, except you would change the 'type' from MPEGVideo to waveaudio. As long as the appropriate codecs are installed it should play your audio.

Below is the source code from my application that played a video on whatever window was in the foreground. Please check the mciSendString documentation and review your 'Open' command for errors.

format PE GUI 4.0
entry start

include 'win32a.inc'

include 'helper.asm'

section '.idata' import data readable writeable

    library kernel32,'KERNEL32.DLL',\
            user32,'USER32.DLL',\
            hook,'HOOK.DLL',\
            winmm,'WINMM.DLL'

    import  hook,\
            SetKeyPressedHandler,'SetKeyPressedHandler'

    import winmm,\
            PlaySound,'PlaySound',\
            mciSendString,'mciSendStringA'

    include 'api\kernel32.inc'
    include 'api\user32.inc'

section '.data' data readable writeable
    _class TCHAR 'PRANK',0
    _title TCHAR 'Video',0

    wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_class

    szmciClose db "close video",0
    szmciOpenTemplate db "open video.mpg type MPEGVideo alias video parent %i style child",0
    szmciPlay db "play video notify",0

    ;String saying what the dll is called.
    szDllName db "HOOK.DLL",0

    ;Name of the function in the dll for the keyboard procedure
    szf_KeyboardProc db "KeyboardProc",0

    ;handle to the dll
    hDll dd ?
    ;handle to the keyboard procedure
    hKeyboardProc dd ?
    ;handle to the hook
    hHook dd ?
    hwnd dd ?

    struct GUITHREADINFO
        cbSize dd ?
        flags dd ?
        hwndActive dd ?
        hwndFocus dd ?
        hwndCapture dd ?
        hwndMenuOwner dd ?
        hwndMoveSize dd ?
        hwndCaret dd ?
        rcCaret RECT
    ends
    kInput KBINPUT

    guithreadinfo GUITHREADINFO

    keyCount dd 0x0 

    ;msg for the message pump
    msg MSG

section '.bss' readable writeable
    szmciOpen rb 100h

section '.text' code readable executable

    start:

        invoke  GetModuleHandle,0
        mov [wc.hInstance],eax
        invoke  LoadIcon,0,IDI_APPLICATION
        mov [wc.hIcon],eax
        invoke  LoadCursor,0,IDC_ARROW
        mov [wc.hCursor],eax
        invoke  RegisterClass,wc
        invoke  CreateWindowEx,0x0,_class,_title,0x0,0x0,0x0,0x0,0x0,NULL,NULL,[wc.hInstance],NULL
        mov [hwnd],eax


        ;Load the DLL into memory.
        invoke LoadLibraryA,szDllName
        cmp eax,0x0
        je exit
        mov [hDll],eax


        invoke GetProcAddress,[hDll],szf_KeyboardProc
        cmp eax,0x0
        je freeLibrary
        mov [hKeyboardProc],eax

        invoke SetKeyPressedHandler,KeyPressedHandler

    hook:
        invoke SetWindowsHookEx,WH_KEYBOARD_LL,[hKeyboardProc],[hDll],0x0
        cmp eax,0x0
        je freeLibrary
        mov [hHook],eax

    msg_loop:
        invoke  GetMessage,msg,NULL,0,0
        cmp eax,1
        jb  unhook
        jne msg_loop
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
    jmp msg_loop

    proc WindowProc hwnd,wmsg,wparam,lparam
    push    ebx esi edi

    cmp [wmsg],0x3B9
    je  .wmnotify
    jmp .defwndproc
    .wmnotify:
    invoke mciSendString,szmciClose,0x0,0x0,0x0
    jmp .done
    .defwndproc:
    invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
    .done:
    pop edi esi ebx
    ret
    endp

    proc KeyPressedHandler code,wparam,lparam
        ;Move the VK Code of the key they pressed into al.
        xor eax,eax
        mov eax,[lparam]
        mov cx,word [eax]

        cmp [wparam],WM_KEYDOWN
        je .ProcessKeyDown
        cmp [wparam],WM_KEYUP
        je .ProcessKeyUp

        .ProcessKeyDown:

            ret ;No need to go any further - we only process characters on key up
        .ProcessKeyUp:
            mov edx,[keyCount]
            inc edx

            cmp cx,VK_F12
            je unhook

            ;Hotkeys.
            ;F12 - Quit.
            cmp edx,0x04
            jne .done
            call PlayVideo
            xor edx,edx
            .done:
            mov [keyCount],edx
        ret
    endp

    proc PlayVideo
        mov [guithreadinfo.cbSize],sizeof.GUITHREADINFO
        invoke GetGUIThreadInfo,NULL,guithreadinfo

        cinvoke wsprintf,szmciOpen,szmciOpenTemplate,[guithreadinfo.hwndFocus]
        invoke mciSendString,szmciOpen,0x0,0x0,0x0
        invoke mciSendString,szmciPlay,0x0,0x0,[hwnd]
        ret
    endp
    unhook:
        invoke UnhookWindowsHookEx,[hHook]

    freeLibrary:
        invoke FreeLibrary,[hDll]
    exit: 
        invoke ExitProcess,0

The above relies on a system-level keyboard hook(Yes, it was a prank, and yes - it was hilarious.)

format PE GUI 4.0 DLL
entry _DllMain

include 'win32a.inc'

section '.data' data readable writeable
    hKeyPressedHandler dd 0x0
section '.text' code readable executable

proc _DllMain hinstDLL,fdwReason,lpvReserved
    mov eax,TRUE
    ret
endp

    proc SetKeyPressedHandler hProc
        mov eax,[hProc]
        mov [hKeyPressedHandler],eax
        ret
    endp

    proc KeyboardProc code,wparam,lparam
        cmp [code],0x0
        jl CallNextHook

        cmp [hKeyPressedHandler],0x0;Make sure our event handler is set.
        je CallNextHook

        ;Call our handler.
        invoke hKeyPressedHandler,[code],[wparam],[lparam]

        CallNextHook:
            invoke CallNextHookEx,0x0,[code],[wparam],[lparam]
            ret
    endp

section '.idata' import data readable writeable

    library kernel32,'KERNEL32.DLL',\
            user32,'USER32.DLL'

    include 'api\kernel32.inc'
    include 'api\user32.inc'

section '.edata' export data readable
    export 'hook.DLL',\
        KeyboardProc,'KeyboardProc',\
        SetKeyPressedHandler,'SetKeyPressedHandler'

section '.reloc' fixups data discardable
0

精彩评论

暂无评论...
验证码 换一张
取 消