# Titanium.Media.Sound

> An object for playing basic audio resources.

- Source: https://titaniumsdk.com/docs/sdk/13.3.1/Titanium.Media.Sound
- Titanium SDK version: 13.3.1
- Kind: proxy
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 0.8
- Extends: Titanium.Proxy
- Inheritance: Titanium.Proxy

The `Sound` object loads the entire media resource in memory before playing.  If you need to 
support streaming, use the [AudioPlayer](https://titaniumsdk.com/docs/sdk/Titanium.Media.AudioPlayer.md) API.

You can control how the sound interacts with other system sounds
by setting [Titanium.Media.audioSessionCategory](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#audioSessionCategory).

Use the [Titanium.Media.createSound](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#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:

* [Android](https://developer.android.com/guide/topics/media/media-formats#core)
* [iOS](https://developer.apple.com/audio/)

## Example: Simple Example

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

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

## Properties

### allowBackground

- Type: Boolean
- Platforms: Android
- Since: Titanium SDK 1.3.0
- Default: `false`

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

### AUDIO_TYPE_ALARM

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Permission: read-only

Used to identify the volume of audio streams for alarms.

### AUDIO_TYPE_MEDIA

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Permission: read-only

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

### AUDIO_TYPE_NOTIFICATION

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Permission: read-only

Used to identify the volume of audio streams for notifications.

### AUDIO_TYPE_RING

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Permission: read-only

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

### AUDIO_TYPE_SIGNALLING

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Permission: read-only

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

### AUDIO_TYPE_VOICE

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Permission: read-only

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

### audioType

- Type: Number
- Platforms: Android
- Since: Titanium SDK 6.2.0
- Default: `<Titanium.Media.AudioPlayer.AUDIO_TYPE_MEDIA>`

Changes the audio-stream-type.

Accepts: `Titanium.Media.Sound.AUDIO_TYPE_ALARM`, `Titanium.Media.Sound.AUDIO_TYPE_MEDIA`, `Titanium.Media.Sound.AUDIO_TYPE_NOTIFICATION`, `Titanium.Media.Sound.AUDIO_TYPE_RING`, `Titanium.Media.Sound.AUDIO_TYPE_SIGNALLING`, `Titanium.Media.Sound.AUDIO_TYPE_VOICE`

### duration

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-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](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#STATE_INITIALIZED)).

### looping

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Default: `false`

Determines whether the audio should loop upon completion.

### paused

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS

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](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#pause)
to pause audio, and [play](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#play) to resume.

### playing

- Type: Boolean
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

Indicates if the audio is playing.

### STATE_BUFFERING

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Audio data is being buffered from the network.

### STATE_INITIALIZED

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Audio playback is being initialized.

### STATE_PAUSED

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Playback is paused.

### STATE_PLAYING

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Audio playback is active.

### STATE_STARTING

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Audio playback is starting.

### STATE_STOPPED

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Audio playback is stopped.

### STATE_STOPPING

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Audio playback is stopping.

### STATE_WAITING_FOR_DATA

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Player is waiting for audio data from the network.

### STATE_WAITING_FOR_QUEUE

- Type: Number
- Platforms: Android
- Since: Titanium SDK 3.2.0
- Permission: read-only

Player is waiting for audio data to fill the queue.

### time

- Type: Number
- Platforms: Android, iPhone, iPad, macOS

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
- Platforms: Android, iPhone, iPad, macOS

URL identifying the audio resource.

### volume

- Type: Number
- Platforms: Android, iPhone, iPad, macOS

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](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#appMusicPlayer) and set the [Titanium.Media.audioSessionCategory](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#audioSessionCategory) property
to either [Titanium.Media.AUDIO_SESSION_CATEGORY_SOLO_AMBIENT](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#AUDIO_SESSION_CATEGORY_SOLO_AMBIENT),
[Titanium.Media.AUDIO_SESSION_CATEGORY_PLAYBACK](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#AUDIO_SESSION_CATEGORY_PLAYBACK),
or [Titanium.Media.AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD](https://titaniumsdk.com/docs/sdk/Titanium.Media.md#AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD).

## Methods

### isLooping

- Platforms: Android, iPhone, iPad, macOS
- DEPRECATED since 10.0.0. Use the <Titanium.Media.Sound.looping> property instead.

Returns the value of the [looping](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#looping) property.

Returns: Boolean

### isPaused

- Platforms: Android, iPhone, iPad, macOS
- DEPRECATED since 10.0.0. Use the <Titanium.Media.Sound.paused> property instead.

Returns the value of the [paused](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#paused) property.

Returns: Boolean

### isPlaying

- Platforms: Android, iPhone, iPad, macOS
- DEPRECATED since 10.0.0. Use the <Titanium.Media.Sound.playing> property instead.

Returns the value of the [playing](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#playing) property.

Returns: Boolean

### pause

- Platforms: Android, iPhone, iPad, macOS

Pauses the audio.

To restart the audio, call [play](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#play).

### play

- Platforms: Android, iPhone, iPad, macOS

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

### release

- Platforms: Android, iPhone, iPad, macOS

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.

### reset

- Platforms: Android, iPhone, iPad, macOS

Resets the audio playback position to the beginning.

### setLooping

- Platforms: Android, iPhone, iPad, macOS
- DEPRECATED since 10.0.0. Set the value of the [looping](Titanium.Media.Sound.looping) property directly.

Sets the value of the [looping](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#looping) property.

Parameters:

- `looping` (Boolean): New value for the `looping` property.

### setPaused

- Platforms: iPhone, iPad, macOS
- DEPRECATED since 10.0.0. It is preferable to use the [pause](Titanium.Media.Sound.pause) and [play](Titanium.Media.Sound.play) methods instead.

Sets the value of the [paused](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#paused) property.

On iOS, this method can be used to pause and unpause playback. For portability,
it is preferable to use the [pause](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#pause) and
[play](https://titaniumsdk.com/docs/sdk/Titanium.Media.Sound.md#play) methods instead.

Parameters:

- `paused` (Boolean): Pass `true` to pause the current playback temporarily, `false` to unpause it.

### stop

- Platforms: Android, iPhone, iPad, macOS

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

## Events

### change

- Platforms: Android
- Since: Titanium SDK 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:

- `state` (Number): Current state of playback.
- `description` (String): Text description of the state of playback.

### complete

- Platforms: Android, iPhone, iPad, macOS

Fired when the audio has finished playing.

Event properties:

- `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 error was generated by the operating system, that system's error value is used. Otherwise, this value will be `-1`.

### error

- Platforms: Android, iPhone, iPad, macOS

Fired when an error occurs while playing the audio.

Event properties:

- `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

- Platforms: iPhone, iPad, macOS

Fired  when audio playback is interrupted by the device.

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

### resume

- Platforms: iPhone, iPad, macOS

Fired when audio playback is resumed after an interruption.

Event properties:

- `interruption` (Boolean): Indicates if the resume was from an interruption.

## Inherited members

7 members are inherited. Each is documented on the type that declares it.

- [Titanium.Proxy](https://titaniumsdk.com/docs/sdk/13.3.1/Titanium.Proxy.md): `addEventListener`, `apiName`, `applyProperties`, `bubbleParent`, `fireEvent`, `lifecycleContainer`, `removeEventListener`

---

- This corpus documents Titanium SDK 13.4.1. Anything here that does not name a version describes 13.4.1.
- Titanium is maintained by TiDev. The Appcelerator toolchain is retired: there is no `appc` CLI, no Appcelerator Studio and no hosted build service. The supported CLI is `titanium` on npm, invoked as `ti`.

