Skip to content
Titanium SDK
Version12.7.0

You are reading 12.7.0. See this page in 13.4.1.

Titanium.Media.Sound

proxyAndroidiPhoneiPadmacOSsince 0.8

Extends Titanium.Proxy

An object for playing basic audio resources.

The Sound object loads the entire media resource in memory before playing. If you need to support streaming, use the AudioPlayer API.

You can control how the sound interacts with other system sounds by setting Titanium.Media.audioSessionCategory.

Use the Titanium.Media.createSound method to create a Sound object. You can play audio in any format supported by the target platform(s), as described in the following documents:

Examples

Simple Example

Simple example of playing a WAVE file from the Resources directory.

var player = Ti.Media.createSound({url:"sound.wav"});
player.play();

Properties 27

allowBackground

BooleanAndroidsince 1.3.0

Determines whether the audio should continue playing even when its activity is paused.

Default: false

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

AUDIO_TYPE_ALARM

NumberAndroidsince 6.2.0read-only

Used to identify the volume of audio streams for alarms.

AUDIO_TYPE_MEDIA

NumberAndroidsince 6.2.0read-only

Used to identify the volume of audio streams for media playback.

AUDIO_TYPE_NOTIFICATION

NumberAndroidsince 6.2.0read-only

Used to identify the volume of audio streams for notifications.

AUDIO_TYPE_RING

NumberAndroidsince 6.2.0read-only

Used to identify the volume of audio streams for the phone ring.

AUDIO_TYPE_SIGNALLING

NumberAndroidsince 6.2.0read-only

Used to identify the volume of audio streams for DTMF tones or beeps.

AUDIO_TYPE_VOICE

NumberAndroidsince 6.2.0read-only

Used to identify the volume of audio streams for voice calls.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

Indicates if the proxy will bubble an event to its parent.

Default: true

duration

Numberread-only

Duration of the audio resource.

On iOS, playback time is reported in seconds.

On Android, time is reported in milliseconds.

Android note: Starting from Titanium 3.2.0, the remote audio plays asynchronously. The duration can only be fetched after the audio is initialized (refer to Titanium.Media.Sound.STATE_INITIALIZED).

looping

Boolean

Determines whether the audio should loop upon completion.

Default: false

paused

Boolean

Indicates if the audio is paused.

On iOS, this property is read-write, and you can pause and resume playback by setting this property to true or false.

On Android, this property is read-only. For portability, use pause to pause audio, and play to resume.

playing

Booleanread-only

Indicates if the audio is playing.

STATE_BUFFERING

NumberAndroidsince 3.2.0read-only

Audio data is being buffered from the network.

STATE_INITIALIZED

NumberAndroidsince 3.2.0read-only

Audio playback is being initialized.

STATE_PAUSED

NumberAndroidsince 3.2.0read-only

Playback is paused.

STATE_PLAYING

NumberAndroidsince 3.2.0read-only

Audio playback is active.

STATE_STARTING

NumberAndroidsince 3.2.0read-only

Audio playback is starting.

STATE_STOPPED

NumberAndroidsince 3.2.0read-only

Audio playback is stopped.

STATE_STOPPING

NumberAndroidsince 3.2.0read-only

Audio playback is stopping.

time

Number

Current playback position of the audio.

Time is reported in milliseconds.

On iOS, prior to Release 3.0, playback time is reported in seconds.

url

String

URL identifying the audio resource.

Methods 14

Inherited from Titanium.Proxy

Adds the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

isLooping

deprecated

Deprecated since 10.0.0

Use the <Titanium.Media.Sound.looping> property instead.

Returns the value of the looping property.

Returns Boolean

isPaused

deprecated

Deprecated since 10.0.0

Use the <Titanium.Media.Sound.paused> property instead.

Returns the value of the paused property.

Returns Boolean

isPlaying

deprecated

Deprecated since 10.0.0

Use the <Titanium.Media.Sound.playing> property instead.

Returns the value of the playing property.

Returns Boolean

Pauses the audio.

To restart the audio, call play.

Starting playing the sound, or resume playing a paused sound.

Releases all internal resources.

This is typically unnecessary but can be useful if you load a large audio file in app.js, and play it only once and you would like to release all resources after your final play to reduce memory.

Inherited from Titanium.Proxy

Removes the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Resets the audio playback position to the beginning.

setLooping

deprecated

Deprecated since 10.0.0

Set the value of the looping property directly.

Sets the value of the looping property.

Parameters
ParametersTypeDescription
loopingBoolean

New value for the looping property.

setPaused

deprecatediPhoneiPadmacOS

Deprecated since 10.0.0

It is preferable to use the pause and play methods instead.

Sets the value of the paused property.

On iOS, this method can be used to pause and unpause playback. For portability, it is preferable to use the pause and play methods instead.

Parameters
ParametersTypeDescription
pausedBoolean

Pass true to pause the current playback temporarily, false to unpause it.

Stops playing the audio and resets the playback position to the beginning of the clip.

Events 5

change

Androidsince 3.2.0

Fired when the state of the playback changes.

This event can be generated by programmatic events, such as pausing or stopping the audio, and also by external events, such as the current state of network buffering.

Event properties
Event propertiesTypeDescription
stateNumber

Current state of playback.

descriptionString

Text description of the state of playback.

Fired when the audio has finished playing.

Event properties
Event propertiesTypeDescription
successBoolean

Indicates if the sound was played successfully. Returns true if request succeeded, false otherwise.

errorString

Error message, if any returned. Will be undefined if success is true.

codeNumber

Error code. Error code will be 0 if success is true, nonzero otherwise. If the error was generated by the operating system, that system's error value is used. Otherwise, this value will be -1.

Fired when an error occurs while playing the audio.

Event properties
Event propertiesTypeDescription
successBoolean

Indicates a successful operation. Returns false.

errorString

Error message, if any returned. May be undefined.

codeNumber

Error code. If the error was generated by the operating system, that system's error value is used. Otherwise, this value will be -1.

messageString

Error message.

interrupted

iPhoneiPadmacOS

Fired when audio playback is interrupted by the device.

Typically called during an interruption due to an incoming phone call.

resume

iPhoneiPadmacOS

Fired when audio playback is resumed after an interruption.

Event properties
Event propertiesTypeDescription
interruptionBoolean

Indicates if the resume was from an interruption.