Skip to content

Titanium.Media.Sound

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:

Extends: Titanium.Proxy · Since: 0.8 · Platforms: android, iphone, ipad, macos

Properties #

allowBackground#

creation only

Type: Boolean

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

apiName#

extended

Type: String

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button
returns Ti.UI.Button.

AUDIO_TYPE_ALARM#

Type: Number

Used to identify the volume of audio streams for alarms.

AUDIO_TYPE_MEDIA#

Type: Number

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

AUDIO_TYPE_NOTIFICATION#

Type: Number

Used to identify the volume of audio streams for notifications.

AUDIO_TYPE_RING#

Type: Number

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

AUDIO_TYPE_SIGNALLING#

Type: Number

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

AUDIO_TYPE_VOICE#

Type: Number

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

audioType#

Type: Number

Changes the audio-stream-type.

bubbleParent#

extended

Type: Boolean

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

Some proxies (most commonly views) have a relationship to other proxies, often
established by the add() method. For example, for a button added to a window, a
click event on the button would bubble up to the window. Other common parents are
table sections to their rows, table views to their sections, and scrollable views
to their views. Set this property to false to disable the bubbling to the proxy's parent.

duration#

Type: Number

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

lifecycleContainer#

extended

Type: Titanium.UI.Window, Titanium.UI.TabGroup

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks
will also be called on the proxy. Proxies that require the activity lifecycle will need this property set
to the appropriate containing Window or TabGroup.

looping#

Type: Boolean

Determines whether the audio should loop upon completion.

paused#

Type: 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#

Type: Boolean

Indicates if the audio is playing.

STATE_BUFFERING#

Type: Number

Audio data is being buffered from the network.

STATE_INITIALIZED#

Type: Number

Audio playback is being initialized.

STATE_PAUSED#

Type: Number

Playback is paused.

STATE_PLAYING#

Type: Number

Audio playback is active.

STATE_STARTING#

Type: Number

Audio playback is starting.

STATE_STOPPED#

Type: Number

Audio playback is stopped.

STATE_STOPPING#

Type: Number

Audio playback is stopping.

STATE_WAITING_FOR_DATA#

Type: Number

Player is waiting for audio data from the network.

STATE_WAITING_FOR_QUEUE#

Type: Number

Player is waiting for audio data to fill the queue.

time#

Type: 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#

Type: String

URL identifying the audio resource.

volume#

Type: Number

Volume of the audio from 0.0 (muted) to 1.0 (loudest).

This setting controls the volume of the sound relative to the overall
volume setting for the device.

On iOS, to adjust the volume of the device, set the volume property of
Titanium.Media.appMusicPlayer and set the Titanium.Media.audioSessionCategory property
to either Titanium.Media.AUDIO_SESSION_CATEGORY_SOLO_AMBIENT,
Titanium.Media.AUDIO_SESSION_CATEGORY_PLAYBACK,
or Titanium.Media.AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD.

Methods #

addEventListener #

extended

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

Parameters:
NameTypeSummaryOptional
nameStringName of the event.No
callbackCallback<Titanium.Event>Callback function to invoke when the event is fired.No

applyProperties #

extended

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that
myproxy[key] = value.

Parameters:
NameTypeSummaryOptional
propsDictionaryA dictionary of properties to apply.No

fireEvent #

extended

Fires a synthesized event to any registered listeners.

Parameters:
NameTypeSummaryOptional
nameStringName of the event.No
eventDictionaryA dictionary of keys and values to add to the Titanium.Event object sent to the listeners.Yes

isLooping #

Returns the value of the looping property.

Returns: Boolean

isPaused #

Returns the value of the paused property.

Returns: Boolean

isPlaying #

Returns the value of the playing property.

Returns: Boolean

pause #

Pauses the audio.

To restart the audio, call play.

play #

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

release #

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.

removeEventListener #

extended

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

Multiple listeners can be registered for the same event, so the
callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function
in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);
Parameters:
NameTypeSummaryOptional
nameStringName of the event.No
callbackCallback<Titanium.Event>Callback function to remove. Must be the same function passed to addEventListener.No

reset #

Resets the audio playback position to the beginning.

setLooping #

Sets the value of the looping property.

Parameters:
NameTypeSummaryOptional
loopingBooleanNew value for the looping property.No

setPaused #

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:
NameTypeSummaryOptional
pausedBooleanPass true to pause the current playback temporarily, false to unpause it.No

stop #

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

Events #

change #

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:
NameTypeSummary
stateNumberCurrent state of playback.
descriptionStringText description of the state of playback.

complete #

Fired when the audio has finished playing.

Event Properties:
NameTypeSummary
successBooleanIndicates if the sound was played successfully.
Returns true if request succeeded, false otherwise.
errorStringError message, if any returned. Will be undefined if success is true.
codeNumberError 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.

error #

Fired when an error occurs while playing the audio.

Event Properties:
NameTypeSummary
successBooleanIndicates a successful operation. Returns false.
errorStringError message, if any returned. May be undefined.
codeNumberError code.
If the error was generated by the operating system, that system's error value
is used. Otherwise, this value will be -1.
messageStringError message.

interrupted #

Fired when audio playback is interrupted by the device.

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

resume #

Fired when audio playback is resumed after an interruption.

Event Properties:
NameTypeSummary
interruptionBooleanIndicates if the resume was from an interruption.

Examples #

Simple Example

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

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

Titanium SDK Documentation