How to play/stop sounds using VBScript on AM 4.6.1 or newer

This forum is meant for people to share their knowledge by writing their own tutorials for Adventure Maker.

Moderators: time-killer-games, Vengeance66, Candle, reneuend, GM-Support

How to play/stop sounds using VBScript on AM 4.6.1 or newer

Postby GM-Support » Sat Dec 25, 2010 12:53 am

Hello,

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
Last edited by GM-Support on Mon Dec 27, 2010 5:48 pm, edited 7 times in total.
GM-Support
Forum Admin and Games Page admin
 
Posts: 2221
Joined: Thu Jun 05, 2003 7:52 pm

Postby Mystery » Sat Dec 25, 2010 12:57 am

Thanks a lot for the information GM! :D

And it's great to see you on the forum :)
User avatar
Mystery
Forum Admin and Games Page admin
 
Posts: 2990
Joined: Sat Feb 04, 2006 8:12 am
Location: Switzerland

Postby reneuend » Sat Dec 25, 2010 5:12 pm

This is PERFECT!!!!

Thanks GM!

I second the statement from Mystery! It's great to see you out here!
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby GM-Support » Mon Dec 27, 2010 5:52 pm

Thanks a lot.

I just updated the first post to remove the two overloads, which, as you pointed out, are not supported by VBScript.

Best regards,
GM-Support
GM-Support
Forum Admin and Games Page admin
 
Posts: 2221
Joined: Thu Jun 05, 2003 7:52 pm


Return to Post Your Own Tutorials

Who is online

Users browsing this forum: No registered users and 0 guests

cron