Class

Sound


Description

Used to play sounds.

Properties

Name

Type

Read-Only

Shared

Pan

Integer

Volume

Integer

Methods

Name

Parameters

Returns

Shared

Clone

Sound

Handle

Ptr

IsPlaying

Boolean

Open

soundFile As FolderItem

Sound

Play

PlayLooping

Stop

Property descriptions


Sound.Pan

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.


Sound.Volume

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.

Method descriptions


Sound.Clone

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= chimes.Clone
mySound.pan = pan.Value
mySound.volume = VolumeValue.Value
mySound.Play

Sound.Handle

Handle As Ptr

Returns a Ptr to the underlying OS Sound object for use with Declares.

Important

This method is not supported for Android.

On iOS, returns a Ptr to the underlying AVAudioPlayer object.


Sound.IsPlaying

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
  chimes.Stop
End If

Sound.Open

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).

On Linux GStreamer, xine, or libsndfile is used (in that order if available). Nil is returned if the Sound can't be read or isn't a Sound file at all.

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)
  s.Play
End If

Sound.Play

Play

Plays the Sound.


Sound.PlayLooping

PlayLooping

Plays the Sound in an infinite loop.

Playlooping can be called instead of Play when you want an indefinite Sound.

chimes.PlayLooping

Sound.Stop

Stop

Stops a Sound that is playing.

Stop can be called to stop a Sound that was played by PlayLooping.

chimes.Stop

Notes

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 Linux, Sound uses GStreamer (requires version 0.10+) by default, which supports a wide range of Sound formats. Xine is used when GStreamer is not available.

On macOS, AVFoundation is used to play sounds with support for a wide variety of Sound formats.

Sample code

This code plays a Sound called "SledgeHammer" which has been added to the project:

SledgeHammer.Play

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
  Try
    Var tada As Sound = Sound.Open(soundFile)
    tada.Play
  Catch error As IOException
    MessageBox("The sound file could not be opened. Error: " + error.Message)
  End Try
End If

The following code plays the Sound "Giggle", which has been added to the project, in an endless loop.

Giggle.PlayLooping

This code stops "Giggle".

Giggle.Stop

Compatibility

All project types on all supported operating systems.

See also

Object parent class; DesktopNotePlayer control.