Titanium.Media.AudioPlayer

Object of Titanium.Media.
Platform Since
Android 0.9
iPhone 0.9
iPad 0.9

Summary

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

Description

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.

Code Examples

Audio Streaming

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

var win = Titanium.UI.createWindow({  
    title:'Audio Test',
    backgroundColor:'#fff',
    layout: 'vertical'
});
 
var startStopButton = Titanium.UI.createButton({
    title:'Start/Stop Streaming',
    top:10,
    width:200,
    height:40
});
 
var pauseResumeButton = Titanium.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();

Methods

Name Summary
addEventListener

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

fireEvent

Fires a synthesized event to any registered listeners.

getAllowBackground

Gets the value of the allowBackground property. (Android only.)

getBitRate

Gets the value of the bitRate property. (iPhone, iPad only.)

getBufferSize

Gets the value of the bufferSize property. (iPhone, iPad only.)

getIdle

Gets the value of the idle property. (iPhone, iPad only.)

getPaused

Returns the value of the paused property. (iPhone, iPad only.)

getPlaying

Returns the value of the playing property. (iPhone, iPad only.)

getProgress

Gets the value of the progress property. (iPhone, iPad only.)

getState

Gets the value of the state property. (iPhone, iPad only.)

getUrl

Returns the value of the url property. (iPhone, iPad only.)

getWaiting

Gets the value of the waiting property. (iPhone, iPad only.)

isPaused

Returns the value of the paused property. (Android only.)

isPlaying

Returns the value of the playing property. (Android only.)

pause

Pauses audio playback.

play

Starts or resumes audio playback. (Android only.)

release

Stops buffering audio data and releases audio resources. (Android only.)

removeEventListener

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

setBitRate

Sets the value of the bitRate property. (iPhone, iPad only.)

setBufferSize

Sets the value of the bufferSize property. (iPhone, iPad only.)

setPaused

Sets the value of the paused property. (iPhone, iPad only.)

setUrl

Sets the value of the url property.

start

Starts or resumes audio playback.

stateDescription

Converts a state value into a text description suitable for display. (iPhone, iPad only.)

stop

Stops audio playback.

Properties

Name Type Summary
STATE_BUFFERING Number

Audio data is being buffered from the network. read-only

STATE_INITIALIZED Number

Audio playback is being initialized. read-only

STATE_PAUSED Number

Playback is paused. read-only

STATE_PLAYING Number

Audio playback is active. read-only

STATE_STARTING Number

Audio playback is starting. read-only

STATE_STOPPED Number

Audio playback is stopped. read-only

STATE_STOPPING Number

Audio playback is stopping. read-only

STATE_WAITING_FOR_DATA Number

Player is waiting for audio data from the network. read-only

STATE_WAITING_FOR_QUEUE Number

Player is waiting for audio data to fill the queue. read-only

allowBackground Boolean

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

bitRate Number

Bit rate of the current playback stream. (iPhone, iPad only.)

bufferSize Number

Size of the buffer used for streaming, in bytes. (iPhone, iPad only.)

idle Boolean

Boolean indicating if the player is idle. (iPhone, iPad only.) read-only

paused Boolean

Boolean indicating if audio playback is paused.

playing Boolean

Boolean indicating if audio is currently playing. read-only

progress Number

Current playback progress, in milliseconds. (iPhone, iPad only.) read-only

state Number

Current state of playback, specified using one of the STATE constants defined on this object. (iPhone, iPad only.) read-only

url String

URL for the audio stream.

waiting Boolean

Boolean indicating if the playback is waiting for audio data from the network. (iPhone, iPad only.) read-only

Events

Name Summary
change

Fired when the state of the playback changes.

progress

Fired once per second with the current progress during playback.