assemblywinapix86masmmasm32

How to understand PlaySound function API document and implement it in MASM?


I know that putting 20001h in PlaySound function as the last parameter can make the music play synchronously. But why is 20001h? I also wanted the music played in the background "repeatedly" but I couldn't understand the API document since it does not mention 20001h stands for SND_SYNC nor show what stands for SND_LOOP. Need some enlightenment, big thank!

This is my code:

includelib Winmm.lib
PlaySound PROTO,
        pszSound:PTR BYTE, 
        hmod:DWORD, 
        fdwSound:DWORD

file BYTE "test.wav",0
SND_SYNC DWORD 0

main proc
    mov SND_SYNC, 20001H 
    INVOKE PlaySound, OFFSET file, NULL, SND_SYNC

.......


Solution

  • I know that putting 20001h in PlaySound function as the last parameter can make the music play synchronously.

    That is incorrect. It plays the file asynchronously.

    The last parameter is a bitmask, it can hold multiple flags OR'ed together. SND_SYNC is defined as 00000h (the absence of any other flags), whereas 20001h is a combination of SND_ASYNC (00001h) OR'ed with SND_FILENAME (20000h).

    SND_LOOP (00008h) can only be used in combination with SND_ASYNC.

    So, to accomplish what you want:

    I also wanted the music played in the background "repeatedly"

    You need to combine the SND_FILENAME, SND_ASYNC, and SND_LOOP flags. That numeric value is 20009h.

    Here are all of the flags you can use with PlaySound(). Their meanings are explained in PlaySound's documentation 1:

    Name Value
    SND_SYNC 0x00000000
    SND_ASYNC 0x00000001
    SND_NODEFAULT 0x00000002
    SND_MEMORY 0x00000004
    SND_LOOP 0x00000008
    SND_NOSTOP 0x00000010
    SND_PURGE 0x00000040
    SND_APPLICATION 0x00000080
    SND_NOWAIT 0x00002000
    SND_ALIAS 0x00010000
    SND_ALIAS_ID 0x00110000
    SND_FILENAME 0x00020000
    SND_RESOURCE 0x00040000
    SND_SENTRY 0x00080000
    SND_RING 0x00100000
    SND_SYSTEM 0x00200000

    1 Note: SND_RING is undocumented, but you can see that SND_ALIAS_ID is a combination of SND_RING OR'ed with SND_ALIAS.