Function help

Hi all,

Think this may be a simple one.

I have the following code:


<? $guestbook = "1.txt";
	$readfile = fopen($guestbook, "r");
	
	$guestbook2 = "2.txt";
	$readfile2 = fopen($guestbook2, "r");
	
	
?>

soundManager.onload = function() {

  var mySound = soundManager.createSound({
    id: 'wind',
    url: '<? echo @fread($readfile, filesize($guestbook)); ?>'
    // onload: [ event handler function object ],
    // other options here..
  });


var myNextSound = soundManager.createSound({
    id: 'bell',
    url: '<? echo @fread($readfile2, filesize($guestbook2)); ?>'
    // onload: [ event handler function object ],
    // other options here..
  });

var myThirdSound = soundManager.createSound({
    id: 'swipe',
    url: ''
    // onload: [ event handler function object ],
    // other options here..
  });


  
  mySound.play({
  onfinish: function() {
    setTimeout(myNextSound.play, 2000); // call myNextSound.play() two seconds after mySound finishes
  }
});
  
}


How do I write the bottom play function so that I can add up to ten? i.e myFourthSound, myFifthSound and so on.

Thanks in advance

That code you posted is what is referred to as “Hard Coded”, wish means each song instance is created in their own variable manually.

What you wanna do is add all the songs to be played to an Array or Object, then this way the play function can go and play one song after in order.


var Count = 0; 
var MySongs = new Array(); 

MySongs.push( { id:'song1', url:'/music/song1.mp3' } ); 
MySongs.push( { id:'song2', url:'/music/song2.mp3' } ); 
MySongs.push( { id:'song3', url:'/music/song3.mp3' } ); 
MySongs.push( { id:'song4', url:'/music/song4.mp3' } ); 
MySongs.push( { id:'song5', url:'/music/song5.mp3' } ); 

function PlaySong()
{
	// play song based on "Count"..
	var Sound = soundManager.createSound( MySongs[Count] ); 
	Sound.play( { onfinish: function(){ setTimeout( 'PlaySong()', 2000 ); } } ); 
	
	// increase or reset the counter..
	Count = ( Count >= MySongs.length ) ? 0 : ( Count + 1 ); 
}

soundManager.onload = PlaySong; 

All you would have to do there is call PlaySong() to start from song1 and it would go up as the songs finish.

Thanks,

That was almost exactly what I needed.

What if I wanted to add a different timeout value to each image in the array though?