Search code examples
winapiassemblyx86masm32playsound

Play a sound file in masm32 and to stop the other sound file at the same time


So I am doing a game in assembly language and I am using the PlaySound() function to play a background song. I want to do that after I "die" in the game, another sound file will start playing a sound file, and at the same time will stop the other background song.


Solution

  • Read the documentation.

    PlaySound function

    pszSound
    A string that specifies the sound to play. The maximum length, including the null terminator, is 256 characters. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.

    ...

    fdwSound
    Flags for playing the sound. The following values are defined.
    ...
    SND_ASYNC
    The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.

    So you need to call PlaySound() three times - one to start the background music, one to stop it, and one to play the next sound.

    invoke PlaySound,offset "the name of the bkgnd file",NULL,SND_ASYNC
    ...
    invoke PlaySound,NULL,NULL,SND_ASYNC
    invoke PlaySound,offset "the name of the other file",NULL,SND_ASYNC