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.
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();
Used to identify the volume of audio streams for alarms.
Used to identify the volume of audio streams for alarms.
Used to identify the volume of audio streams for media playback.
Used to identify the volume of audio streams for media playback.
Used to identify the volume of audio streams for notifications.
Used to identify the volume of audio streams for notifications.
Used to identify the volume of audio streams for the phone ring.
Used to identify the volume of audio streams for the phone ring.
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.
Used to identify the volume of audio streams for voice calls.
Used to identify the volume of audio streams for voice calls.
Audio data is being buffered from the network.
Audio data is being buffered from the network.
Audio playback is being initialized.
Audio playback is being initialized.
Player is waiting for audio data from the network.
Player is waiting for audio data from the network.
Player is waiting for audio data to fill the queue.
Player is waiting for audio data to fill the queue.
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
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
.
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
Changes the audio-stream-type.
This API can be assigned the following constants:
Default: AUDIO_TYPE_MEDIA
Bit rate of the current playback stream.
Bit rate of the current playback stream.
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
Size of the buffer used for streaming, in bytes.
Size of the buffer used for streaming, in bytes.
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.
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.
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.
Boolean indicating if audio playback is paused.
Boolean indicating if audio playback is paused.
Boolean indicating if audio is currently playing.
Boolean indicating if audio is currently playing.
Returns false
if playback is stopped or paused.
Current playback progress, in milliseconds.
Current playback progress, in milliseconds.
Returns zero if bitRate
has not yet been detected.
Current state of playback, specified using one of the STATE
constants defined on this object.
This API can be assigned the following constants:
Current playback position of the audio.
Current playback position of the audio.
Time is reported in milliseconds.
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.
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.
Adds the specified callback as an event listener for the named event.
Name of the event.
Callback function to invoke when the event is fired.
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.
A dictionary of properties to apply.
Fires a synthesized event to any registered listeners.
Name of the event.
A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.
Returns the audio session id.
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.
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.
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 of the event.
Callback function to remove. Must be the same function passed to addEventListener
.
Sets the value of the allowBackground property.
New value for the property.
Sets the value of the audioFocus property.
New value for the property.
Sets the value of the audioType property.
New value for the property.
Sets the value of the bitRate property.
New value for the property.
Sets the value of the bubbleParent property.
New value for the property.
Sets the value of the bufferSize property.
New value for the property.
Sets the value of the lifecycleContainer property.
New value for the property.
Sets the value of the paused property.
New value for the property.
Sets the value of the time property.
New value for the property.
Sets the value of the volume property.
New value for the property.
Converts a state value into a text description suitable for display.
State value to convert.
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.
Current state of playback.
This API can be assigned the following constants:
Text description of the state of playback.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.
Fired when the audio has finished playing.
Indicates if the sound was played successfully.
Returns true
if request succeeded, false
otherwise.
Error message, if any returned. Will be undefined if success
is true
.
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 that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.
Fired when there's an error.
Error message.
Error code. Different between android and iOS.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.
Fired once per second with the current progress during playback.
Current progress, in milliseconds.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.