Skip to content

Instantly share code, notes, and snippets.

@rajubd49
Created April 16, 2015 13:01
Show Gist options
  • Save rajubd49/f4c30975eae5036dddac to your computer and use it in GitHub Desktop.
Save rajubd49/f4c30975eae5036dddac to your computer and use it in GitHub Desktop.
How to play audio randomly - Appcelerator Titanium
var win = Ti.UI.createWindow({
backgroundColor : '#000',
layout:'vertical'
});
var mp3_array = [];
mp3_array.push('music/sound1.mp3');
mp3_array.push('music/sound2.mp3');
mp3_array.push('music/sound3.mp3');
Titanium.Media.audioSessionMode = Titanium.Media.AUDIO_SESSION_MODE_AMBIENT;
var current_index = 0;
function playSound(index){
var sound = Titanium.Media.createSound();
sound.url = mp3_array[index];
sound.play();
Ti.API.info('Now Playing '+index+': '+mp3_array[index]);
sound.addEventListener('complete',function(){
current_index++;
if(current_index < mp3_array.length){
playSound(current_index);
}
else {
Ti.API.info('all sounds complete!');
}
});
}
playSound(current_index);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment