AS 2 [RESOLVED] creating a uniform delay of n seconds between the sounding of two words.
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [RESOLVED] creating a uniform delay of n seconds between the sounding of two words.

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    29

    resolved [RESOLVED] creating a uniform delay of n seconds between the sounding of two words.

    hi all !

    So I have another problem that I am stuck with.

    I have the sound files of the following words.

    pre, vent, ed, lift, seed, as

    pre.mp3, vent.mp3, ed.mp3, lift.mp3 etc

    and I wish to use these to make the computer to call out the following words.

    prevented, lifted, seeded, preceded. So I have broken these as:

    1. pre-vent-ed
    2. lift-ed
    3. seed-ed
    4 pre-seed-ed ( This would be the same sound as preceded)

    I have stored each of these words in a string which i convert into an array by exploding it before passing the array to
    a pn_sound function.

    The pn_sound function checks to see if all words have been called out and if not calls another function ok_sound

    ok_sound takes the sound array for each word for e.g. for pre-vent-ed it would have the sounds arr[0] = pre, arr[1]=vent, arr[2] = ed.

    It then creates a new sound object (s = new sound() and attaches the first sound ( arr[0] = pre) to it,

    s.attachsound(arr[0]);
    s.start // and plays the sound.

    s.onsoundcomplete then checks to see if there are more bytes in the array to complete the sound of the word. If so it attaches the next byte and calls s.start again to play the sound thus forming a recursive function.

    Thus onsoundcomplete recurses till all bytes have been played and the sound for the word completed.

    Now is the time to fetch the next word and play it and so on till all words have been played.

    What I want is that irrespective of the time it takes to play each word, THERE SHOULD BE A CONSTANT GAP BETWEEN ANY 2 WORDS. so
    once a words has been played, the program should wait for say 3 or 4 seconds before it fetches the next word and plays it.

    Please can someone show me how I can achieve this.

    Thanks loads all !

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,110
    Hi,

    Why not attach your file as it is now and we can try and work on it, save alot of trying to work out how you have it set up so far.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Hi fruitbeard and all,

    Please find attached the file I have been working on. I have tried to achieve a uniform delay in many ways but could not achieve it. I have tried using setInterval and setTimeOut and their combinations too. Maybe I over-tried it I guess.

    So what this attached file is doing is that it generates a 2.5 sec delay and calls the words during that delay. If the word is long it takes a longer time to call out and so the gap between the calling out of two words is uneven.

    I want that two words, no matter their size, should have a constant delay between their calling out.

    Thanks all and especially fruitbeard.
    Attached Files Attached Files

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,110
    Hi,

    I'm sure you can get this working from this extra added code, if it doesn't already.
    PHP Code:
    var iter:Number 0;
    var 
    word:Array = new Array();

    word[0] = "list,ed";
    word[1] = "pre,vent,pre,seed,ed";
    word[2] = "list,ed";
    word[3] = "pre,seed,ed";
    word[4] = "pre,list,ed";

    var 
    noOfWords:Number word.length;

    pnSound();

    function 
    pSound():Void
    {
        
    clearInterval(playsndID);

        var 
    tempStr:String word[iter];
        
    sBArr tempStr.split(",");

        
    //trace("tempStr = "+tempStr);
        
    sBCt 0;
        
    snd = new Sound();
        
    snd.attachSound(sBArr[sBCt]);
        
    snd.start();

        
    snd.onSoundComplete = function()
        {
            if (
    sBCt sBArr.length 1)
            {
                
    sBCt++;
                
    snd.attachSound(sBArr[sBCt]);
                
    snd.start();
            }
        };
        
        var 
    startTime getTimer();
        var 
    currentTime startTime;

        
    onEnterFrame = function ()
        {
            
    currentTime getTimer();
            if (
    currentTime startTime >= 2500)
            {
                
    trace(currentTime);
                
    trace("ok");
                
    startTime getTimer();
                
    currentTime startTime;
                
    delete onEnterFrame;
                
    iter++;
                
    playnsID setInterval(pnSound2500);
            }
        };
    }


    function 
    pnSound():Void
    {
        
    clearInterval(playnsID);

        
    trace("pnS");

        if (
    iter == noOfWords)
        {
            return;
        }

        if (
    iter <= noOfWords)
        {
            
    playsndID setInterval(pSound200);// if not the last byte calls 
        
    }


  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,110
    Hi,

    perhaps this might be better, I know you can do it from at least a mix of the two codes,
    PHP Code:
    var iter:Number 0;
    var 
    word:Array = new Array();
    var 
    delay:Number;

    word[0] = "list,ed";
    word[1] = "pre,vent,pre,seed,ed";
    word[2] = "list,ed";
    word[3] = "pre,seed,ed";
    word[4] = "pre,list,ed";

    var 
    noOfWords:Number word.length;

    pnSound();

    function 
    pSound():Void
    {
        
    clearInterval(playsndID);

        var 
    tempStr:String word[iter];
        
    sBArr tempStr.split(",");

        
    //trace("tempStr = "+tempStr);
        
    sBCt 0;
        
    snd = new Sound();
        
    snd.attachSound(sBArr[sBCt]);
        
    snd.start();

        
    delay 0;

        
    holdID setInterval(hold1);
        
    snd.onSoundComplete = function()
        {
            if (
    sBCt sBArr.length 1)
            {
                
    sBCt++;
                
    snd.attachSound(sBArr[sBCt]);
                
    snd.start();
            }
            else
            {
                
    clearInterval(holdID);
                
    iter++;
                
    playnsID setInterval(pnSound2500 delay);
            }
        };
    }

    function 
    hold():Void
    {
        
    delay++;
        
    trace(delay);
    }

    function 
    pnSound():Void
    {
        
    clearInterval(playnsID);

        
    trace("pnS");

        if (
    iter == noOfWords)
        {
            return;
        }

        if (
    iter <= noOfWords)
        {
            
    playsndID setInterval(pSound200);
        }


  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,110
    Hi,

    Me again, I think this one might have it!!!
    PHP Code:
    var iter:Number 0;
    var 
    word:Array = new Array();
    var 
    delay:Number;

    word[0] = "list,ed";
    word[1] = "pre,vent,pre,seed,ed";
    word[2] = "list,ed";
    word[3] = "pre,seed,ed";
    word[4] = "pre,list,ed";

    var 
    noOfWords:Number word.length;

    pnSound();

    function 
    pSound():Void
    {
        
    clearInterval(playsndID);

        var 
    tempStr:String word[iter];
        
    sBArr tempStr.split(",");

        
    //trace("tempStr = "+tempStr);
        
    sBCt 0;
        
    snd = new Sound();
        
    snd.attachSound(sBArr[sBCt]);
        
    snd.start();

        
    delay 0;

        
    holdID setInterval(hold1);
        
    snd.onSoundComplete = function()
        {
            if (
    sBCt sBArr.length 1)
            {
                
    sBCt++;
                
    snd.attachSound(sBArr[sBCt]);
                
    snd.start();
            }
            else
            {
                
    clearInterval(holdID);
                
    iter++;
                
    playnsID setInterval(pnSound2500 delay);
            }
        };
    }

    function 
    hold():Void
    {
        
    delay++;
        
    trace(delay);
    }

    function 
    pnSound():Void
    {
        
    clearInterval(playnsID);

        if (
    iter == noOfWords)
        {
            
    trace("Completed");
            return;
        }

        if (
    iter noOfWords)
        {
            
    playsndID setInterval(pSound200);
        }
        
    trace("pnS");


  7. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Hi fruitbeard,

    Thanks loads for the replies. I just logged in and wow ! 3 replies from you. Thanks so much. I'll look into them tomorrow since it's very late now and revert with my implementation. I am sure that I would not need to do much since I am sure you would have done it best already.

    Thanks loads for all your help. I appreciate it much.

    P.S. On a lighter note, if it took you 3 times to perfect this one, I now don't mind the 15 odd different tries that I made in trying to do this right and still couldn't.

  8. #8
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Hi fruitbeard,

    Thanks Loads, I have gone through the final version and yes it works great. That was a nice idea to add the time-delay of the word sound to delay of 2500 between words. I had thought of this but i was hoping that there was some pure wait or delay function that could be used directly which apparently there isn't. Yet I think the final version was really short and sweet.

    Thanks loads, I really appreciate your help.

Tags for this Thread

Posting Permissions

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




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com