# Preload sounds / Play on key press

**URL:** <https://forum.kirupa.com/t/preload-sounds-play-on-key-press/245342>\
**Category:** flash\
**Created:** [November 28, 2007, 6:58pm UTC](https://forum.kirupa.com/t/preload-sounds-play-on-key-press/245342 "2007-11-28T18:58:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bardic](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bardic/32/3708_2.png) [@bardic](https://forum.kirupa.com/u/bardic)\
**Post date:** [November 28, 2007, 6:58pm UTC](https://forum.kirupa.com/t/preload-sounds-play-on-key-press/245342/1 "2007-11-28T18:58:41Z")

</div>

Ok. Trying to preload all the sounds, and after loading allow the user to hit keys on their keyboard to make the noise related to the key. But I am stuck. The preloader works but when I hit the key it loads it again. And when I put it on the server the preloader stops working.

```auto

Stage.align = "TL";
Stage.scaleMode = "noscale";

var letterArr:Array = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V"); 
var soundArr:Array = new Array("27328_hello_flowers_Doom_Bug_1.mp3",
                               "27329_hello_flowers_Doom_Bug_2.mp3",
                               "27330_hello_flowers_Suck_Slush_1.mp3",
                               "27331_hello_flowers_Suck_Slush_2.mp3",
                               "27333_hello_flowers_Suck_Slush_5.mp3",
                               "27409_hello_flowers_The_Lonely_Bug.mp3",
                               "27410_hello_flowers_Beep_716698.mp3",
                               "27412_hello_flowers_bent_BirdBubbles.mp3",
                               "27413_hello_flowers_bent_squish.mp3",
                               "27415_hello_flowers_glitchem_44390.mp3",
                               "30910_hello_flowers_b01090.mp3",
                               "30911_hello_flowers_b01091.mp3",
                               "30912_hello_flowers_b01092.mp3",
                               "30943_hello_flowers_t_3310.mp3",
                               "30944_hello_flowers_t_3311.mp3",
                               "30945_hello_flowers_t_3312.mp3",
                               "30946_hello_flowers_t_3313.mp3",
                               "30947_hello_flowers_t_3314.mp3",
                               "32282_hello_flowers_hf_Noise_voice_2.mp3",
                               "32283_hello_flowers_hf_Noise_voice_3.mp3",
                               "32516_hello_flowers_Alo_rotay_Bent_toy_.mp3"
                               ); 

i=0;
home = this;

var glitch:Sound = new Sound();
var keyListener:Object = new Object();
var txt:TextField = this.createTextField("txtf",this.getNextHighestDepth(),0,0,0,0);
txt.autoSize = "left";

function loadNew(){
    glitch.loadSound("sounds/"+soundArr*,false);
}

loadNew();

this.onEnterFrame=function(){
    if(glitch.getBytesLoaded() <= glitch.getBytesTotal()){
        txt.text = "Loading sound "+ (i+1) +" / "+ soundArr.length+ " : " + Math.ceil(glitch.getBytesLoaded()/glitch.getBytesTotal() * 100) + "%";
    }
    
    glitch.onLoad = function(success){
        glitch.stop();        
        if(i>=soundArr.length-1){
            delete home.onEnterFrame;
            txt.text = "Type away!";
        }else{
            i++;
            loadNew();
        }
    }
}

keyListener.onKeyDown = function() {
    for(i=0;i<letterArr.length;i++){        
        if(String.fromCharCode(Key.getCode()) == letterArr*){            
            glitch.onLoad = function(success){                
                glitch.start();
            }            
            glitch.loadSound("sounds/"+soundArr*,false);
        }
    }
}

keyListener.onKeyUp = function(){
    glitch.stop();
}
Key.addListener(keyListener);

```
