Please find below the code to play and stop sound files using VBScript on AM 4.6.1 or newer:
- Code: Select all
Sub PlaySound(SoundFile, NumRepeat, ChannelNumber)
If ChannelNumber < 9 Then
'With VBScript it is currently not possible to interact with the 8 first audio channels.
MsgBox "The specified audio channel is invalid.", vbCritical, "Error"
Exit Sub
End If
If Action.IsComponentInstalled("NSPlay.NSPlayCtl.1") = False Then
MsgBox "Unable to find the media player component.", vbCritical, "Error"
Else
If Action.DoesComponentExist("MediaPlayer" + CStr(ChannelNumber)) = false Then
AddComponent "NSPlay.NSPlayCtl.1", "MediaPlayer" + CStr(ChannelNumber)
End If
Component("MediaPlayer" + CStr(ChannelNumber)).Object.FileName = SoundFile
Component("MediaPlayer" + CStr(ChannelNumber)).Object.PlayCount = NumRepeat
Component("MediaPlayer" + CStr(ChannelNumber)).Object.Play
End If
End Sub
Sub StopSound(ChannelNumber)
If ChannelNumber < 9 Then
'With VBScript it is currently not possible to interact with the 8 first audio channels.
MsgBox "The specified audio channel is invalid.", vbCritical, "Error"
Exit Sub
End If
If Action.DoesComponentExist("MediaPlayer" + CStr(ChannelNumber)) = true Then
Component("MediaPlayer" + CStr(ChannelNumber)).Object.Stop
End If
End Sub
You must copy/paste the code above into the "VBScript Global Procedures" window (click "VBS Procedures" from the Project Menu of Adventure Maker).
To use the code, simply call the "PlaySound" and "StopSound" methods from anywhere in your project (for example, from the "Advanced" tab of the Hotspot Properties).
The parameters of the "PlaySound" method are:
- SoundFile: The location ("path") and the filename of the sound that you wish to play. For example: GetPath(4) + "MySound1.wav"
- NumRepeat: Number of times that you wish the sound to be played. To loop the sound forever, enter 0. If you don't specify any number, the sound will play only once.
- ChannelNumber: Any number bigger than 9. To play multiple sounds simultaneously, use different numbers. To stop playing the sound using the "StopSound" method, you will need to specify the channel number.
Here are some examples:
- Code: Select all
PlaySound GetPath(4) + "Sound1.wav", 1, 9
PlaySound GetPath(4) + "Sound2.wav", 1, 10
PlaySound GetPath(4) + "Sound3.wav", 1, 11
PlaySound GetPath(4) + "Sound4.wav", 0, 12
Note: "GetPath(4)" means that the file is located under the "Sound Files" category of the "Manage Resources" window (which corresponds to the "Areas" sub-folder of your project folder). To be sure that the sound will play even after the project is compiled (when most file names are scrambled - unless you uncheck the corresponding option in the "Distribution Options" window), you should put the files in the "External" subfolder that is inside the project folder, and use "GetPath(2)" instead of "GetPath(4)". More information about the "GetPath" method can be found at the following URL:
http://adventuremaker.com/help/vbscript_reference.htm
If the sound file is not found, you will just get an obscure error message that says "Automation error Unspecified error"
The parameter of the "StopSound" method is the "ChannelNumber" that you used to play the sound that you want to stop.
Here are some examples:
- Code: Select all
StopSound 9
StopSound 10
StopSound 11
StopSound 12
Please note that it is currently not possible from VBScript to interact with the sounds played through the "Audio" tab of the Hotspot Properties and Frame Properties windows (i.e. the first 7 channels).
Best regards,
GM-Support