[Previous] [Table of Contents] [Next]

Playing Sound

WSH doesn't support the playing of sound files. In Advanced Development with Microsoft Windows Script Host 2.0, I introduce some methods implemented in an ActiveX control for playing sound. However, there are a few other ways that you can play sound in a WSH script. One common approach is to use the Windows ActiveMovie Player. This player installs an ActiveX control called Amovie.ocx that you can use as follows:

Set oWSH = WScript.CreateObject("WScript.Shell")
oWSH.Run "RunDll32.exe %WINDIR%\System\Amovie.ocx,RunDll " _
         & "/play /close C:\Test\Sound.wav"

The second command shells out a RunDll32 call that invokes the ActiveMovie Player. The parameters submitted in the command line force the player to play the sound file and close the window automatically.

Amovie.ocx doesn't ship with Windows 2000. Instead, Windows 2000 comes with the file Msdxm.ocx, which does the same thing. So you can use the following command:

Set oWSH = WScript.CreateObject("WScript.Shell")
oWSH.Run "RunDll32.exe %WINDIR%\System32\Msdxm.ocx,RunDll " _
         & "/play /close C:\Test\Sound.wav"

This command invokes Windows Media Player and plays the specified media file (sound or movie). Note that the .ocx file is located in the \System32 folder in Windows 2000 and in the \System subfolder in Windows 98.

The VBScript sample in Listing 14-15 uses the GetSpecialFolder method of the FileSystemObject object to retrieve the path to the Windows system folder. It then plays a .wav file.

Listing 14-15 PlaySound.vbs

'************************************************
' File:    PlaySound.vbs (WSH sample in VBScript) 
' Author:  (c) G. Born 
'
' Playing sound using Msdxm.ocx
'************************************************
Option Explicit

Dim fso, oWSH
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set oWSH = WScript.CreateObject("WScript.Shell")

oWSH.Run "RunDll32.exe " & fso.GetSpecialFolder(1) & _
         "\Msdxm.ocx,RunDll " & _
         "/play /close " & _
         "C:\WSHDevGuide\Chapter14\The Microsoft Sound.wav"

'*** End

Using Internet Explorer to Play Sound

You can also use Internet Explorer to play a sound file independent of the platform. The sound file is contained in an HTML file and plays in the background as the HTML file loads. The code to process such a sound file is shown here:

<html>
    <head>
        <bgsound src="The Microsoft Sound.wav" loop="1">
    </head>
    <body>
    </body>
</html>

The <bgsound> tag plays a background sound. The sound file is specified in the src attribute. To use a script to launch the browser, load the HTML code, and play the sound file, you use the following code:

Set oIE = WScript.CreateObject("InternetExplorer.Application")
oIE.Navigate GetPath() & "Sound.htm"  ' HTML page with sound

The browser window remains invisible because you don't need it. (oIE.Visible = 0 is the default setting.) Therefore, you need not set the properties for browser window size and style. The two code lines shown above are sufficient.

There's just one problem: the script must close the browser, but the browser runs as an independent process, so the script doesn't know when the sound file has been played. In Chapter 13, you learned how to use the Sleep method to suspend the script for a given amount of time. You can use this technique to pause the script, during which time Internet Explorer is assumed to have played the sound file. When the script resumes execution, it can call the Quit method to terminate the browser. However, if you use the Sleep method, the user can't stop playing the sound.

A better solution uses the Popup method:

oWSH.Popup "Hello, world", 10

The second parameter forces Popup to close the dialog box after 10 seconds. An open dialog box also suspends script execution, so the program incurs no CPU load during that time.

If the user doesn't want to listen to the sound, he or she can click the OK button to close the dialog box. This causes the script to call the browser's Quit method, as shown below, which interrupts the playing of the sound and terminates the browser.

oIE.Quit

The small VBScript program in Listing 14-16 launches Internet Explorer, loads the Sound.htm file, and suspends script execution for a specified time period. The browser window remains hidden; the user sees only two dialog boxes, one that delays the script and one that reports the end of the program.

Listing 14-16 PlaySound1.vbs

'*************************************************
' File:    PlaySound1.vbs (WSH sample in VBScript) 
' Author:  (c) G. Born
'
' Using Internet Explorer 4 or 5 to play a sound 
' file and suspending the script using the Popup
' method
'*************************************************
Option Explicit

Const delay = 10      ' Suspend time in seconds

Dim oIE, oWSH         ' Declare variables.
Dim path

' Create WshShell object reference (for Popup).
Set oWSH = WScript.CreateObject("WScript.Shell")

' Launch Internet Explorer.
Set oIE = WScript.CreateObject("InternetExplorer.Application")
oIE.Navigate GetPath() & "Sound.htm"  ' HTML page with sound
oIE.Visible = 0         ' Keep invisible.

' Important: Wait until IE is ready.
 Do While (oIE.Busy): Loop

' Delay until the sound is played.
oWSH.Popup "Script suspended for " & delay & " seconds" _
           & vbCrLf & "because we're playing sound." & vbCrLf & _
           "Click OK to cancel sound", delay, _
           "Sound demo", vbInformation + vbSystemModal

oIE.Quit             ' Close Internet Explorer.

MsgBox "Finished playing sound", vbInformation + vbSystemModal
 
' Helper function
Function GetPath()
    ' Retrieve script path.
    Dim path
    path = WScript.ScriptFullName
    GetPath = Left(path, InStrRev(path, "\"))
End Function

'*** End