The Sound object lets you work with sound in an application. The Sound lets you create a Sound object, load and play an external audio file into that object, close the sound stream, and access data about the sound, such as information about the number of bytes in the stream and ID3 metadata. More detailed control of the sound is performed through the sound source - the SoundChannel object for the sound - and through the properties in the SoundTransform object that control the output of the sound to the device's speakers.

The sound is played as it is retrieved from a ByteArray object that you populate with sound data.


Sound object inherited from EventDispatcher


To create Sound object use new method:

Lib.Media.Audio.Sound.new(stream, context, forcePlayAsMusic, inEngine)

Creates a new Sound object. If you pass a stream valid URLRequest object to the Sound constructor, the constructor automatically calls the load() function for the Sound object. If you do not pass a valid URLRequest object to the Sound constructor, you must call the load() function for the Sound object yourself, or the stream will not load.

Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.

Engines using for audio depending on platform:

iOS and Mac - openal

Windows - SDL audio

Android - java access to native media player.

Use .getEngine() method for more info

Best practice is to load audio from ByteArray object (check examples)

stream - URLRequest object (can be nil)

context - SoundLoaderContext object (not supported set to nil)

forcePlayAsMusic - some engines support loading audio files as music object

inEngine - can be nil for autodetection or you can specify what sound engine you want to use opensl, openal, etc. recomended value nil.




Created Sound object has properties:

bytesLoaded

Returns the currently available number of bytes in this sound object. This property is usually useful only for externally loaded files.


bytesTotal

Returns the total number of bytes in this sound object.


id3

Provides access to the metadata that is part of an audio file.

   album:String;

   artist:String;

   comment:String;

   genre:String;

   songName:String;

   track:String;

   year:String;

(not supported)


isBuffering

Returns the buffering state of external audio files. If the value is true, any playback is currently suspended while the object waits for more data.


length

The length of the current sound in milliseconds.


url

The URL from which this sound was loaded. This property is applicable only to Sound objects that were loaded using the Sound.load() method. 



and methods:

getEngine()

Return engine name used for audio object


close()

Closes the stream, causing any download of data to cease. No data may be read from the stream after the close() method is called.


load(stream, context, forcePlayAsMusic, inEngine)

Initiates loading of an external audio file from the specified URL. (not supported)

stream - URLRequest object (not supported, set to nil)

context - SoundLoaderContext object (not supported set to nil)

forcePlayAsMusic - some engines support loading audio files as music object

inEngine - can be nil for autodetection or you can specify what sound engine you want to use opensl, openal, etc. recomended value nil.

loadCompressedDataFromByteArray(bytes, length, forcePlayAsMusic, inEngine)

load audio sound data from a ByteArray object into a Sound object.

The data will be read from the current bytes ByteArray position and will leave the ByteArray position at the end of the specified bytes length once finished.

bytes - ByteArray audio data

length - bytes length

forcePlayAsMusic - some engines support loading audio files as music object

inEngine - can be nil for autodetection or you can specify what sound engine you want to use opensl, openal, etc. recomended value nil.

loadPCMFromByteArray(bytes, samples, format, stereo, sampleRate, inEngine)

Load PCM 32-bit floating point sound data from a bytes ByteArray object into a Sound object.

The data will be read from the current ByteArray position and will leave the ByteArray position at the end of the specified sample length multiplied by either 1 channel or 2 channels if the stereo flag is set once finished.

bytes - ByteArray audio data

samples - not supported

format - 'float' or 'short' audio format (default recommended: 'float')

stereo - flag true/false (default recommended: true)

sampleRate - sample rate (default recommended: 44100)

inEngine - can be nil for autodetection or you can specify what sound engine you want to use opensl, openal, etc. recomended value nil.

play(startTime, loops, sndTransform)

Generates a new SoundChannel object to play back the sound. This method returns a SoundChannel object, which you access to stop the sound and to monitor volume.(To control the volume, panning, and balance, access the SoundTransform object assigned to the sound channel.)

startTime - startTime The initial position in milliseconds at which playback should start.

loops - Defines the number of times a sound loops back to the startTime value before the sound channel stops playback.

sndTransform - The initial SoundTransform object assigned to the sound channel


Examples:


Audio = Lib.Media.Audio

Text = Lib.Media.Text

Events = Lib.Media.Events

Display = Lib.Media.Display

BitmapData = Display.BitmapData

Bitmap = Display.Bitmap

Geom = Lib.Media.Geom

stage = Display.stage


sysName = Lib.Media.System.systemName()

desktop = true

if sysName == "android" or sysName == "ios" then

       desktop = false

end


function playMusic(path)

       local bytes = Lib.Project.getBytes(path)

       local sound = Audio.Sound.new(nil, nil, false, nil)

       sound.loadCompressedDataFromByteArray(bytes, bytes.length, true, nil)

       channel = sound.play(0, 1, nil)

       if channel == nil then

               print("Could not play '" .. path .. "' on this system.")

       else

               channel.addEventListener(Events.Event.SOUND_COMPLETE, 

                       function (e)

                               print("Complete - play " .. path)

                       end, false, 0, false)

       end

end


local bmp = Bitmap.new(BitmapData.loadFromBytes(Lib.Project.getBytes("/Sound/data/drum_kit.jpg"), nil), Display.PixelSnapping.AUTO, true)

stage.addChild(bmp)


if desktop then

       playMusic("/Sound/data/Party_Gu-Jeremy_S-8250_hifi.ogg")

else

       playMusic("/Sound/data/Party_Gu-Jeremy_S-8250_hifi.mp3")

end

Created with the Personal Edition of HelpNDoc: Full-featured Help generator