Used to play sounds.
soundFile As FolderItem
Pan As Integer
Specifies the relative volume (balance) between the left and right speakers.
The range is from -100 to +100, with 0 indicating equal volume in the left and right channels. -100 plays Sound in the left channel only; 100 plays Sound in the right channel only. The default is 0.
Volume As Integer
Controls the volume of the Sound.
The range is from 0 to 100; 0 mutes the Sound and 100 plays the Sound at the "normal" volume set in the computer's volume setting. Default is 100.
Clone As Sound
Returns a clone of the Sound as a Sound, but can be played, stopped, and modified independently of the original Sound.
Use Clone instead of opening the same Sound file twice, because Clone is likely to be substantially more efficient.
This example clones a Sound that has been added to the project and plays it.
Var mySound As New Sound
mySound.pan = pan.Value
mySound.volume = VolumeValue.Value
Handle As Ptr
This method is not supported for Android.
On iOS, returns a Ptr to the underlying AVAudioPlayer object.
IsPlaying As Boolean
Returns True if the Sound is playing.
This example stops a Sound that was played using PlayLooping. It checks to see if the Sound is still playing.
If chimes.IsPlaying Then
Open(soundFile As FolderItem) As Sound
Opens the FolderItem to be read and returns its contents as a Sound object.
This method is shared.
On macOS, the Sound class recognizes Sound formats that Core Audio can play.
The Open method is not support for iOS.
On Windows WAVs, WMAs, MP3s, and MIDI files can be played, but only WAVs play with DirectSound (this allows individual pan and volume settings for each Sound. You can also play multiple sounds simultaneously).
This example loads a Sound file called “hello.mp3” from the desktop into a Sound object and plays it.
Var f As FolderItem = SpecialFolder.Desktop.Child("hello.mp3")
If f <> Nil And f.Exists Then
Var s As Sound
s = s.Open(f)
Plays the Sound.
Plays the Sound in an infinite loop.
Playlooping can be called instead of Play when you want an indefinite Sound.
Stops a Sound that is playing.
Stop can be called to stop a Sound that was played by PlayLooping.
Sounds that have been added to the project can be accessed via their object name. Sounds can also be loaded from disk by calling the Open method of a Sound.
Sound can play Sound formats and simultaneous sounds as determined by the system.
On Windows, Sound can play a variety of Sound formats but can only play WAV sounds simultaneously.
On macOS, AVFoundation is used to play sounds with support for a wide variety of Sound formats.
This code plays a Sound called "SledgeHammer" which has been added to the project:
This code loads a Sound file called "TaDa.mp3" from the desktop into a Sound object and plays it.
Var soundFile As FolderItem = SpecialFolder.Desktop.Child("TaDa.mp3")
If soundFile.Exists Then
Var tada As Sound = Sound.Open(soundFile)
Catch error As IOException
MessageBox("The sound file could not be opened. Error: " + error.Message)
The following code plays the Sound "Giggle", which has been added to the project, in an endless loop.
This code stops "Giggle".
All project types on all supported operating systems.