How Can I Play a Sound From Within a Script?

How Can I Play a Sound From Within a Script?

  • Comments 14
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script that pops up a message box to alert users to a problem. Is there any way I can play a sound at the same time the message box appears?

-- TL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TL. You guys do this on purpose, don’t you; you love to ask questions where there isn’t a clear-cut answer. This is yet another case of that: you can search high and low through the VBScript and the WSH documentation, but you won’t find any sort of method that allows you to directly play a sound from within a script, not so much as a peep. Or beep, as the case may be.

Because of that, you’ll have to call another utility in order to play a sound. As long as your sound is in the .WAV format, your best bet is to use the Windows Sound Recorder. There are at least two reasons for that. For one thing, the Sound Recorder is lightweight and reasonably quick to load; Media Player is far more powerful, but is probably overkill just to play a beep. For another, by passing the appropriate command-line parameters, you can run Sound Recorder invisibly; as a result, you (or the user) will hear the sound, but won’t see Sound Recorder onscreen.

Here’s a script that will play Notify.wav, a standard operating system sound found in the Windows\Media folder:

strSoundFile = "C:\Windows\Media\Notify.wav"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True

The first two lines of the script are pretty simple. In the first line, we just assign the file path to the variable strSoundFile; in the second line, we create an instance of the Wscript Shell object, the object we’ll use to actually run the Sound Recorder.

Things get a tiny bit trickier in line 3. To run Sound Recorder from the command-prompt, we need to type in this command:

sndrec32 /play /close "C:\Windows\Media\Notify.wav"

The first two command-line parameters tell Sound Recorder to play the file and then terminate itself; the third parameter, of course, is the name of the file we want to play. In this example, we don’t need to enclose the file path in double quotes; that’s only required if there are spaces in the path name. We went ahead and enclosed it in double quotes to give you a template you can use when playing a sound file that does have a space in the path name. Thus:

sndrec32 /play /close "C:\Windows\Media\Windows XP Error.wav"

In order to enclose the file path in double quotes, we use the Chr(34) command, which, well, inserts a double quote in our string. Hence this line of code, which strings together sndrec32 /play /close, a double quote mark () the file C:\Windows\Media\Notify.wav, and another double quote mark ():

strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)

Finally, in line 4 we use the Run method, passing along the command string we just constructed. The 0 parameter causes Sound Recorder to run in a hidden window; the True tells the script to wait until the sound finishes playing before resuming.

As for playing a sound at the same time a message box appears, well, you’ll find that close-to-impossible; that’s because it takes a second or two for the Sound Recorder to load and start playing. Your best bet is to do two things. First, use the False parameter when calling the Sound Recorder; that will tell the script to start the recorder and then continue on, without waiting until the sound finishes playing.

Second, use the Wscript.Sleep command to pause the script for a second or so; that will give the Sound Recorder time to load and - with luck - the sound will start playing right about the time the message box appears. Will it start playing at exactly the same time the message box appears? Probably not. But at least you can come close.

Here’s a sample script that plays a sound and - at about the same time - pops up a message box as well:

strSoundFile = "C:\windows\Media\Notify.wav"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, False
Wscript.Sleep 1000
Msgbox "A problem has occurred."
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • sndrec32 does not exist on Windows 7 64-bit.  I expanded SNDREC32.EX_ from my 2003 media.  It has issues doing that.

  • The 32 in "sndrec32" means it is a 32-bit application. What you have there is a 64-bit operating system. You need to do a little research to see if Windows 7 has an equivalent app. If not, you're out of luck.

    If I was sitting at my Windows 7 box, I would give you some hints of where to look.

    Good luck!

  • Thank You very much - it is working

  • How do I take this code and use it? It seems it will work for what I need, but I am not familiar with how to take the code and implement it.

  • There is the SAPI way to play wav file

    s of course:

    '[Tested on Windows XP SP3]

    Set oVoice = CreateObject("SAPI.SpVoice")

    set oSpFileStream = CreateObject("SAPI.SpFileStream")

    oSpFileStream.Open "c:\Windows\Media\tada.wav"

    oVoice.SpeakStream oSpFileStream

    oSpFileStream.Close

    oVoice.Speak "Finished Playing wave file."

  • Daz, I like your way better. Is there a way to keep it from telling me it "finished playing wav file"?

  • Haha. Nevermind, I see it.

  • Cahge this line:

    oVoice.Speak "Finished Playing wave file."

    TO

    oVoice.Speak "Don't say finished Playing wave file."

    COmputers are dumba nd have to be told everything.

  • Nope!  The SAPI one didn't work on Windows 7.  Bummer!

  • thanks Boss...

    ur code is working on my file...

  • Thanks, I used this script to play a sound whenever a new visitor arrives in our website and it works like a charm. You guys rock

  • Thanks this helped me ALOT!!!!!!!

  • Hi! Great solution! Very often happens I miss important phone calls when I listen to music to loud or have forgotten turn on sound after comming home... Original method doesn't work on my windows8, but I used this SAPI to pair up with Android Notifier plus say it loud, what is happening - call or too low battery... Fun coding!