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#
Type: Boolean
Determines whether the audio should continue playing even when its activity is paused.
apiName#
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_SIGNALLING#
Type: Number
Used to identify the volume of audio streams for DTMF tones or beeps.
bubbleParent#
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#
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.
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.
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 #
Adds the specified callback as an event listener for the named event.
| Name | Type | Summary | Optional |
|---|---|---|---|
name | String | Name of the event. | No |
callback | Callback<Titanium.Event> | Callback function to invoke when the event is fired. | No |
applyProperties #
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.
| Name | Type | Summary | Optional |
|---|---|---|---|
props | Dictionary | A dictionary of properties to apply. | No |
fireEvent #
Fires a synthesized event to any registered listeners.
| Name | Type | Summary | Optional |
|---|---|---|---|
name | String | Name of the event. | No |
event | Dictionary | A dictionary of keys and values to add to the Titanium.Event object sent to the listeners. | Yes |
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 #
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);
| Name | Type | Summary | Optional |
|---|---|---|---|
name | String | Name of the event. | No |
callback | Callback<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.
| Name | Type | Summary | Optional |
|---|---|---|---|
looping | Boolean | New 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.
| Name | Type | Summary | Optional |
|---|---|---|---|
paused | Boolean | Pass 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.
| Name | Type | Summary |
|---|---|---|
state | Number | Current state of playback. |
description | String | Text description of the state of playback. |
complete #
Fired when the audio has finished playing.
| Name | Type | Summary |
|---|---|---|
success | Boolean | Indicates if the sound was played successfully. Returns true if request succeeded, false otherwise. |
error | String | Error message, if any returned. Will be undefined if success is true. |
code | Number | Error code. Error code will be 0 if success is true, nonzero otherwise. If the errorwas 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.
| Name | Type | Summary |
|---|---|---|
success | Boolean | Indicates a successful operation. Returns false. |
error | String | Error message, if any returned. May be undefined. |
code | Number | Error code. If the error was generated by the operating system, that system's error value is used. Otherwise, this value will be -1. |
message | String | Error 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.
| Name | Type | Summary |
|---|---|---|
interruption | Boolean | Indicates 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();