Yes, you can play a sound!
The following code is actually written by
fatfreechicken, in his awesome Portal credits easter egg of SessionSaver, but it can be used to play almost
any music file. All credits for this code go to him!
- First, you'll need to get the short path of your music file. This can be done with the following code and should be executed at script start-up or when the window is created. Don't forget to define the sMusicPath variable globally!
code:
var sLongPath = MsgPlus.ScriptFilesPath+"\\MerryXmas.mp3"; //The long path to the file. It's recommended that you put the music file in your script directory if you want to distribute your script later on.
var lpszShortPath = Interop.Allocate((255+1)*2); //A buffer for the short path.
Interop.Call("kernel32", "GetShortPathNameW", sLongPath, lpszShortPath, 255); //Call the GetShortPathName function to store the short path in our buffer.
sMusicPath = lpszShortPath.ReadString(0); //Get the contents of the buffer. This is the variable containing the short path which we can use to play the sound.
- Next, you need to play the sound. This can be done right after you executed the code above, but you can also do this when the user clicks a button. Anyway, it's up to you to decide where to place the code:
code:
Interop.Call("winmm.dll", "mciSendStringW", "play "+sMusicPath, 0, "", 0); //Start playing the sound.
- Eventually, you want to stop the sound a bit earlier, or make sure it's stopped when the window is closed. This is done by simply replacing the "play" command with "stop":
code:
Interop.Call("winmm.dll", "mciSendStringW", "stop "+sMusicPath, 0, "", 0); //Stop the sound from playing.
And there you have it!