如何使用mciSendString播放高品质的音乐(320kpbs)

时间:2011-01-21 07:33:23

标签: winapi

我尝试使用mciSendString播放高质量的音乐(320kpbs),但它不起作用。并且mciSendString返回值是MCIERR_INVALID_DEVICE_NAME(263)。怎么解决?

1 个答案:

答案 0 :(得分:0)

之前我曾使用mciSendString在您当前打开的任何应用程序的窗口上呈现mpeg视频。播放音频文件几乎完全相同,除非您将“类型”从MPEGVideo更改为waveaudio。只要安装了适当的编解码器,它就应该播放你的音频。

以下是我的应用程序中的源代码,该代码在前台的任何窗口上播放视频。请查看mciSendString文档并查看“打开”命令以查找错误。

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

上面依赖于系统级的键盘钩子(是的,这是一个恶作剧,是的 - 这很有趣。)

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