SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Function help

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function help

    Hi all,

    Think this may be a simple one.

    I have the following code:

    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

  2. #2
    SitePoint Enthusiast rainner's Avatar
    Join Date
    Apr 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    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.

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •