Titanium.Media.AudioPlayer
> Titanium.Media.AudioPlayer

An audio player object used for streaming audio to the device, and low-level control of the audio playback.

On Android, when you are done playing a given audio file, you must call the release method to stop buffering audio data and release associated system resources.

On iOS, you can control how the audio stream interacts with other system sounds by setting Titanium.Media.audioSessionMode.

Use the Titanium.Media.createAudioPlayer method to create an audio player.

Examples

Audio Streaming

The following example demonstrates using the AudioPlayer object to stream audio.

var win = Ti.UI.createWindow({
    title: 'Audio Test',
    backgroundColor: '#fff',
    layout: 'vertical'
});

var startStopButton = Ti.UI.createButton({
    title: 'Start/Stop Streaming',
    top: 10,
    width: 200,
    height: 40
});

var pauseResumeButton = Ti.UI.createButton({
    title: 'Pause/Resume Streaming',
    top: 10,
    width: 200,
    height: 40,
    enabled: false
});

win.add(startStopButton);
win.add(pauseResumeButton);

// allowBackground: true on Android allows the
// player to keep playing when the app is in the
// background.
var audioPlayer = Ti.Media.createAudioPlayer({
    url: 'www.example.com/podcast.mp3',
    allowBackground: true
});

startStopButton.addEventListener('click',function() {
    // When paused, playing returns false.
    // If both are false, playback is stopped.
    if (audioPlayer.playing || audioPlayer.paused) {
        audioPlayer.stop();
        pauseResumeButton.enabled = false;
        if (Ti.Platform.name === 'android')
        {
            audioPlayer.release();
        }
    } else {
        audioPlayer.start();
        pauseResumeButton.enabled = true;
    }
});

pauseResumeButton.addEventListener('click', function() {
    if (audioPlayer.paused) {
        audioPlayer.start();
    } else {
        audioPlayer.pause();
    }
});

audioPlayer.addEventListener('progress', function(e) {
    Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
});

audioPlayer.addEventListener('change', function(e) {
    Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
});

win.addEventListener('close',function() {
    audioPlayer.stop();
    if (Ti.Platform.osname === 'android')
    {
        audioPlayer.release();
    }
});

win.open();
  • 0.9
  • 0.9
  • 0.9
Defined By

Properties

Titanium.Media.AudioPlayer
AUDIO_TYPE_ALARM : Numberreadonly

Used to identify the volume of audio streams for alarms.

Used to identify the volume of audio streams for alarms.

  • 6.2.0
Titanium.Media.AudioPlayer
AUDIO_TYPE_MEDIA : Numberreadonly

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

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

  • 6.2.0
Titanium.Media.AudioPlayer
AUDIO_TYPE_NOTIFICATION : Numberreadonly

Used to identify the volume of audio streams for notifications.

Used to identify the volume of audio streams for notifications.

  • 6.2.0
Titanium.Media.AudioPlayer
AUDIO_TYPE_RING : Numberreadonly

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

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

  • 6.2.0
Titanium.Media.AudioPlayer
AUDIO_TYPE_SIGNALLING : Numberreadonly

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

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

  • 6.2.0
Titanium.Media.AudioPlayer
AUDIO_TYPE_VOICE : Numberreadonly

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

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

  • 6.2.0
Titanium.Media.AudioPlayer
STATE_BUFFERING : Numberreadonly

Audio data is being buffered from the network.

Audio data is being buffered from the network.

Titanium.Media.AudioPlayer
STATE_INITIALIZED : Numberreadonly

Audio playback is being initialized.

Audio playback is being initialized.

Titanium.Media.AudioPlayer
STATE_PAUSED : Numberreadonly

Playback is paused.

Playback is paused.

Titanium.Media.AudioPlayer
STATE_PLAYING : Numberreadonly

Audio playback is active.

Audio playback is active.

Titanium.Media.AudioPlayer
STATE_STARTING : Numberreadonly

Audio playback is starting.

Audio playback is starting.

Titanium.Media.AudioPlayer
STATE_STOPPED : Numberreadonly

Audio playback is stopped.

Audio playback is stopped.

Titanium.Media.AudioPlayer
STATE_STOPPING : Numberreadonly

Audio playback is stopping.

Audio playback is stopping.

Titanium.Media.AudioPlayer
STATE_WAITING_FOR_DATA : Numberreadonly

Player is waiting for audio data from the network.

Player is waiting for audio data from the network.

Titanium.Media.AudioPlayer
STATE_WAITING_FOR_QUEUE : Numberreadonly

Player is waiting for audio data to fill the queue.

Player is waiting for audio data to fill the queue.

Titanium.Media.AudioPlayer
: BooleanCreation-Only
Boolean to indicate if audio should continue playing even if the associated Android Activity is paused. ...

Boolean to indicate if audio should continue playing even if the associated Android Activity is paused.

Setting allowBackground to true allows the audio to continue playing, for example, if the application is in the background.

Default: false

  • 0.9
apiName : Stringreadonly

The name of the API that this proxy corresponds to.

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.

  • 3.2.0
  • 3.2.0
  • 3.2.0
Titanium.Media.AudioPlayer
: BooleanCreation-Only
Focuses on the current audio player and stops other audio playing. ...

Focuses on the current audio player and stops other audio playing.

If true then all other audio sources will be stopped when Titanium.AudioPlayer is started or resumed.

Default: false

  • 0.9
Titanium.Media.AudioPlayer
bitRate : Number

Bit rate of the current playback stream.

Bit rate of the current playback stream.

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

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.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0
Titanium.Media.AudioPlayer
bufferSize : Number

Size of the buffer used for streaming, in bytes.

Size of the buffer used for streaming, in bytes.

  • 0.9
  • 0.9
Titanium.Media.AudioPlayer
duration : Numberreadonly

Estimated duration in milliseconds of the file being played.

Estimated duration in milliseconds of the file being played.

May return 0 when playing a live stream.

  • 3.3.0
  • 3.3.0
  • 3.3.0
Titanium.Media.AudioPlayer
idle : Booleanreadonly

Boolean indicating if the player is idle.

Boolean indicating if the player is idle.

true if the player is in the initialized state: that is, not playing, paused, or waiting for data.

  • 0.9
  • 0.9

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

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.

  • 3.6.0
Titanium.Media.AudioPlayer
paused : Boolean

Boolean indicating if audio playback is paused.

Boolean indicating if audio playback is paused.

Titanium.Media.AudioPlayer
playing : Booleanreadonly

Boolean indicating if audio is currently playing.

Boolean indicating if audio is currently playing.

Returns false if playback is stopped or paused.

Titanium.Media.AudioPlayer
progress : Numberreadonly

Current playback progress, in milliseconds.

Current playback progress, in milliseconds.

Returns zero if bitRate has not yet been detected.

  • 0.9
  • 0.9
Titanium.Media.AudioPlayer
: Numberreadonly
Current state of playback, specified using one of the STATE constants defined on this object. ...

Current state of playback, specified using one of the STATE constants defined on this object.

This API can be assigned the following constants:

  • 0.9
  • 0.9
Titanium.Media.AudioPlayer
time : Number

Current playback position of the audio.

Current playback position of the audio.

Time is reported in milliseconds.

  • 3.3.0
Titanium.Media.AudioPlayer
url : String

URL for the audio stream.

URL for the audio stream.

Titanium.Media.AudioPlayer
volume : Number

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

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.audioSessionMode property to either Titanium.Media.AUDIO_SESSION_CATEGORY_AMBIENT, Titanium.Media.AUDIO_SESSION_CATEGORY_SOLO_AMBIENT, or Titanium.Media.AUDIO_SESSION_CATEGORY_PLAYBACK.

  • 2.1.0
  • 2.1.0
  • 2.1.0
Titanium.Media.AudioPlayer
waiting : Booleanreadonly

Boolean indicating if the playback is waiting for audio data from the network.

Boolean indicating if the playback is waiting for audio data from the network.

This property is true if the player is in any of the waiting states, including buffering, starting, waiting for data, and waiting for queue.

  • 0.9
  • 0.9
Defined By

Methods

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

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

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Applies the properties to the proxy. ...

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.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

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

Returns

  • void
Titanium.Media.AudioPlayer
( ) : Boolean
Gets the value of the allowBackground property. ...

Gets the value of the allowBackground property.

  • 0.9

Returns

  • Boolean
Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Titanium.Media.AudioPlayer
( ) : Boolean
Gets the value of the audioFocus property. ...

Gets the value of the audioFocus property.

  • 0.9

Returns

  • Boolean
Titanium.Media.AudioPlayer
( ) : Number
Returns the audio session id. ...

Returns the audio session id.

  • 5.4.0

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the audioType property. ...

Gets the value of the audioType property.

  • 6.2.0

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the bitRate property. ...

Gets the value of the bitRate property.

  • 0.9
  • 0.9

Returns

  • Number
Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the bufferSize property. ...

Gets the value of the bufferSize property.

  • 0.9
  • 0.9

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the duration property. ...

Gets the value of the duration property.

  • 3.3.0
  • 3.3.0
  • 3.3.0

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Boolean
Gets the value of the idle property. ...

Gets the value of the idle property.

  • 0.9
  • 0.9

Returns

  • Boolean
Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.Media.AudioPlayer
( ) : Boolean
Gets the value of the paused property. ...

Gets the value of the paused property.

Returns

  • Boolean
Titanium.Media.AudioPlayer
( ) : Boolean
Gets the value of the playing property. ...

Gets the value of the playing property.

Returns

  • Boolean
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the progress property. ...

Gets the value of the progress property.

  • 0.9
  • 0.9

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the state property. ...

Gets the value of the state property.

  • 0.9
  • 0.9

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the time property. ...

Gets the value of the time property.

  • 3.3.0

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : String
Gets the value of the url property. ...

Gets the value of the url property.

Returns

  • String
Titanium.Media.AudioPlayer
( ) : Number
Gets the value of the volume property. ...

Gets the value of the volume property.

  • 2.1.0
  • 2.1.0
  • 2.1.0

Returns

  • Number
Titanium.Media.AudioPlayer
( ) : Boolean
Gets the value of the waiting property. ...

Gets the value of the waiting property.

  • 0.9
  • 0.9

Returns

  • Boolean
Titanium.Media.AudioPlayer
( ) : Boolean
Returns the value of the paused property. ...

Returns the value of the paused property.

  • 0.9

Returns

  • Boolean
Titanium.Media.AudioPlayer
( ) : Boolean
Returns the value of the playing property. ...

Returns the value of the playing property.

  • 0.9

Returns

  • Boolean
Titanium.Media.AudioPlayer
( )
Pauses audio playback. ...

Pauses audio playback.

On iOS, the pause call operates as a toggle. If the stream is already paused, calling pause again resumes playing the stream.

On Android, the pause call does nothing if the stream is already paused.

On both platforms, calling start on a paused stream resumes play.

Returns

  • void
Titanium.Media.AudioPlayer
( )
Starts or resumes audio playback. ...

Starts or resumes audio playback.

This method is identical to start.

  • 0.9

Returns

  • void
Titanium.Media.AudioPlayer
( )
Stops buffering audio data and releases audio resources. ...

Stops buffering audio data and releases audio resources.

On Android, this method should be called when you are done streaming a given audio object, to release underlying resources, including buffered data.

  • 0.9

Returns

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

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

  • name : String

    Name of the event.

  • callback : Callback<Object>

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

Returns

  • void
Titanium.Media.AudioPlayer
( allowBackground )
Sets the value of the allowBackground property. ...

Sets the value of the allowBackground property.

  • 0.9

Parameters

  • allowBackground : Boolean

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( audioFocus )
Sets the value of the audioFocus property. ...

Sets the value of the audioFocus property.

  • 0.9

Parameters

  • audioFocus : Boolean

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( audioType )
Sets the value of the audioType property. ...

Sets the value of the audioType property.

  • 6.2.0

Parameters

  • audioType : Number

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( bitRate )
Sets the value of the bitRate property. ...

Sets the value of the bitRate property.

  • 0.9
  • 0.9

Parameters

  • bitRate : Number

    New value for the property.

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( bufferSize )
Sets the value of the bufferSize property. ...

Sets the value of the bufferSize property.

  • 0.9
  • 0.9

Parameters

  • bufferSize : Number

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void
Titanium.Media.AudioPlayer
( paused )
Sets the value of the paused property. ...

Sets the value of the paused property.

Parameters

  • paused : Boolean

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( time )
Sets the value of the time property. ...

Sets the value of the time property.

  • 3.3.0

Parameters

  • time : Number

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( url )
Sets the value of the url property. ...

Sets the value of the url property.

Parameters

  • url : String

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( volume )
Sets the value of the volume property. ...

Sets the value of the volume property.

  • 2.1.0
  • 2.1.0
  • 2.1.0

Parameters

  • volume : Number

    New value for the property.

Returns

  • void
Titanium.Media.AudioPlayer
( )
Starts or resumes audio playback. ...

Starts or resumes audio playback.

Returns

  • void
Titanium.Media.AudioPlayer
( state ) : String
Converts a state value into a text description suitable for display. ...

Converts a state value into a text description suitable for display.

  • 0.9
  • 0.9

Parameters

  • state : Number

    State value to convert.

Returns

  • String
Titanium.Media.AudioPlayer
( )
Stops audio playback. ...

Stops audio playback.

Returns

  • void
Defined By

Events

Titanium.Media.AudioPlayer
Fired when the state of the playback changes. ...

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.

Properties

Titanium.Media.AudioPlayer
Fired when the audio has finished playing. ...

Fired when the audio has finished playing.

  • 0.9

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.

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

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

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.Media.AudioPlayer
Fired when there's an error. ...

Fired when there's an error.

  • 4.1.0
  • 4.1.0
  • 4.1.0

Properties

  • error : String

    Error message.

  • code : Number

    Error code. Different between android and iOS.

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

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

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.Media.AudioPlayer
Fired once per second with the current progress during playback. ...

Fired once per second with the current progress during playback.

Properties

  • progress : String

    Current progress, in milliseconds.

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

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

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •