Go To Next Song?

You must Login before you can answer or comment on any questions.

Hello, i have found this question in Q&A, but this isn't go to next song? i have tried everything, but can't make it:(

var win = Ti.UI.currentWindow;
var mp3_array = [];
 
mp3_array.push('1.mp3');
mp3_array.push('2.mp3');
mp3_array.push('3.mp3');
mp3_array.push('4.mp3');
mp3_array.push('5.mp3');
 
Titanium.Media.audioSessionMode = Titanium.Media.AUDIO_SESSION_MODE_AMBIENT;
var sound = Titanium.Media.createSound();   
var current_index = 0;
 
function playSound(index){
 
    sound.url = mp3_array[index];
    sound.play();
 
}
 
sound.addEventListener('complete',function(){
 
    // increment the array index
    Ti.API.info("Song Finished,Go To Next Song");
    Ti.API.info(current_index);
    current_index++;
 
    if(current_index < mp3_array.length){
 
        // play the next mp3 in the array 
        playSound(current_index);
    } else {
          Ti.API.info('all sounds complete!');
        }
 
});
 
// play the first mp3 in the array @ index 0
playSound(current_index);
win.open();

3 Answers

Accepted Answer

Try this:

var songs = []
    // push your sounds into the array
 
    var index = 0;
    loopMessageSound();
 
    function loopMessageSound() {
        messageSound = Ti.Media.createSound();
        messageSound.url = songs[index].sound;
        messageSound.addEventListener('complete', function(e) {
            index ++;   
            if (index < songs.length)
                loopMessageSound();
        });
        messageSound.play();
    }

Thank you very much :)), exactly it works like this

var win = Ti.UI.currentWindow;
var songs = []
songs.push('1.mp3');
songs.push('2.mp3');
songs.push('3.mp3');
    // push your sounds into the array
 Ti.API.info(songs.length);
    var index = 0;
    loopMessageSound();
 
    function loopMessageSound() {
        messageSound = Ti.Media.createSound();
        // i have deleted the .sound , is this important???
        messageSound.url = songs[index];
        Ti.API.info(messageSound.url);
                messageSound.addEventListener('complete', function(e) {
            index ++;   
            if (index < songs.length)
                loopMessageSound();
        });
        messageSound.play();
    }
 
win.open();
how can i turn to the first song when the songs finished?

— answered 10 months ago by Graham Jeffrey
answer permalink
9 Comments
  • Correct, the .sound has to be removed. I simplified the code taken from my app where the array contains objects and the sound is a property.

    — commented 10 months ago by alessandro la rocca

  • To turn to the first song at the end of the loop you have to play with the index.

    — commented 10 months ago by alessandro la rocca

  • thank you very much, i will try that and notice if something happens

    — commented 10 months ago by Graham Jeffrey

  • Show 6 more comments

Your Answer

Think you can help? Login to answer this question!