Hi, recently a friend of mine has asked me if I could create a program for him in flash which would be able to play sounds corresponding to keys on the keyboard, but split in to 3 groups, and only 1 sound from each group can be playing at a time. I don’t know that much about as3, and I told him this but said I would try anyway. The first thing I came up with was this:
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.getDefinitionByName;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
import flash.net.URLRequest;
function setup() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, useKeypress);
}
setup();
function useKeypress (keyEvent:KeyboardEvent) {
var current_pressed:Number=keyEvent.keyCode; //converting the key code in to a stored number as current_pressed
useKey(current_pressed); //passing information to useKey
}
function useKey(key_pressed:Number) {
var group1_key_Array:Array = [112, 113, 114, 49, 50, 51, 81, 87, 69, 65, 83, 68, 90, 88, 67]; //these are the keycodes as flash interprets them
var group2_key_Array:Array = [115, 116, 117, 52, 53, 54, 82, 84, 89, 70, 71, 72, 86, 66, 78];
var group3_key_Array:Array = [118, 119, 120, 55, 56, 57, 85, 73, 79, 74, 75, 76, 77, 188, 190];
var searchForKey = function (searchValue) {
trace("searching for: "+searchValue+" in group1_key_Array of array length: "+group1_key_Array.length);
for (var i=0; i < group1_key_Array.length; i++) {
if (searchValue == group1_key_Array*) {
trace("found "+searchValue+" at array # "+i+" in group1");
var play_group1:Number = group1_key_Array.indexOf(key_pressed);
playSound1(play_group1);
}
}
trace("searching for: "+searchValue+" in group2_key_Array of array length: "+group2_key_Array.length);
for (i=0; i < group2_key_Array.length; i++) {
if (searchValue == group2_key_Array*) {
trace("found "+searchValue+" at array # "+i+" in group 2");
var play_group2:Number = group2_key_Array.indexOf(key_pressed);
playSound2(play_group2);
}
}
trace("searching for: "+searchValue+" in group3_key_Array of array length: "+group3_key_Array.length);
for (i=0; i < group3_key_Array.length; i++) {
if (searchValue == group3_key_Array*) {
trace("found "+searchValue+" at array # "+i+" in group3");
var play_group3:Number = group3_key_Array.indexOf(key_pressed);
playSound3(play_group3);
}
}
};
//call the above function to search for the key pressed in one of the groups
searchForKey(key_pressed);
}
var snd_g1_chnl:SoundChannel=new SoundChannel();
function playSound1(keyLet:Number) {
snd_g1_chnl.stop();
var snd_group1:Sound=new Sound();
//create array referencing sounds
var sounds1:Array = new Array("f1.mp3", "f2.mp3", "f3.mp3", "1.mp3", "2.mp3", "3.mp3", "q.mp3", "w.mp3", "e.mp3", "a.mp3", "s.mp3", "d.mp3", "z.mp3", "x.mp3", "c.mp3");
snd_group1.load(new URLRequest(sounds1[keyLet]));
snd_g1_chnl = snd_group1.play();
snd_g1_chnl.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
playSound1(keyLet);
trace("The sound1 has finished playing, loop");
}
}
var snd_g2_chnl:SoundChannel=new SoundChannel();
function playSound2(keyLet:Number) {
snd_g2_chnl.stop();
var snd_group2:Sound=new Sound();
//create array referencing sounds
var sounds2:Array = new Array("f4.mp3", "f5.mp3", "f6.mp3", "4.mp3", "5.mp3", "6.mp3", "r.mp3", "t.mp3", "y.mp3", "f.mp3", "g.mp3", "h.mp3", "v.mp3", "b.mp3", "n.mp3");
snd_group2.load(new URLRequest(sounds2[keyLet]));
snd_g2_chnl = snd_group2.play();
snd_g2_chnl.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
playSound2(keyLet);
trace("The sound2 has finished playing, loop");
}
}
var snd_g3_chnl:SoundChannel=new SoundChannel();
function playSound3(keyLet:Number) {
snd_g3_chnl.stop();
var snd_group3:Sound=new Sound();
//create array referencing sounds
var sounds3:Array = new Array("f7.mp3", "f8.mp3", "f9.mp3", "7.mp3", "8.mp3", "9.mp3", "u.mp3", "i.mp3", "o.mp3", "j.mp3", "k.mp3", "l.mp3", "m.mp3", ",.mp3", "..mp3");
snd_group3.load(new URLRequest(sounds3[keyLet]));
snd_g3_chnl = snd_group3.play();
snd_g3_chnl.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
playSound3(keyLet);
trace("The sound3 has finished playing, loop");
}
}
However, it turned out that the sounds didn’t start instantly, and there was lag in between loops something which I was afraid might happen. I thought I should then probably try loading all of the sounds first then playing them as appropriate, however, since I’m new to as3 I didn’t have a very elegant solution to this, so I ended up trying this:
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.getDefinitionByName;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
import flash.net.URLRequest;
function setup() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, useKeypress);
}
setup();
function useKeypress (keyEvent:KeyboardEvent) {
var current_pressed:Number=keyEvent.keyCode; //converting the key code in to a stored number as current_pressed
playSound1(current_pressed); //passing information to useKey
}
//Create a new SoundChannel Object
var snd_g1_chnl:SoundChannel=new SoundChannel();
//create array referencing sounds
var sounds1:Array = new Array("f1.mp3", "f2.mp3", "f3.mp3", "1.mp3", "2.mp3", "3.mp3", "q.mp3", "w.mp3", "e.mp3", "a.mp3", "s.mp3", "d.mp3", "z.mp3", "x.mp3", "c.mp3");
//load every single sound
var snd_group1_f1:Sound=new Sound();
snd_group1_f1.load(new URLRequest(sounds1[0])); //f1 112
var snd_group1_f2:Sound=new Sound();
snd_group1_f2.load(new URLRequest(sounds1[1])); //f2 113
var snd_group1_f3:Sound=new Sound();
snd_group1_f3.load(new URLRequest(sounds1[2])); //f3 114
var snd_group1_1:Sound=new Sound();
snd_group1_1.load(new URLRequest(sounds1[3])); //1 49
var snd_group1_2:Sound=new Sound();
snd_group1_2.load(new URLRequest(sounds1[4])); //2 50
var snd_group1_3:Sound=new Sound();
snd_group1_3.load(new URLRequest(sounds1[5])); //3 51
var snd_group1_q:Sound=new Sound();
snd_group1_q.load(new URLRequest(sounds1[6])); //q 81
var snd_group1_w:Sound=new Sound();
snd_group1_w.load(new URLRequest(sounds1[7])); //w 87
var snd_group1_e:Sound=new Sound();
snd_group1_e.load(new URLRequest(sounds1[8])); //e 69
var snd_group1_a:Sound=new Sound();
snd_group1_a.load(new URLRequest(sounds1[9])); //a 65
var snd_group1_s:Sound=new Sound();
snd_group1_s.load(new URLRequest(sounds1[10])); //s 83
var snd_group1_d:Sound=new Sound();
snd_group1_d.load(new URLRequest(sounds1[11])); //d 68
var snd_group1_z:Sound=new Sound();
snd_group1_z.load(new URLRequest(sounds1[12])); //z 90
var snd_group1_x:Sound=new Sound();
snd_group1_x.load(new URLRequest(sounds1[13])); //x 88
var snd_group1_c:Sound=new Sound();
snd_group1_c.load(new URLRequest(sounds1[14])); //c 67
function playSound1(keyLet:Number) {
if (keyLet == 112) {
snd_g1_chnl = snd_group1_f1.play();
}
if (keyLet == 113) {
snd_g1_chnl = snd_group1_f2.play();
}
if (keyLet == 114) {
snd_g1_chnl = snd_group1_f3.play();
}
if (keyLet == 49) {
snd_g1_chnl = snd_group1_1.play();
}
if (keyLet == 50) {
snd_g1_chnl = snd_group1_2.play();
}
if (keyLet == 51) {
snd_g1_chnl = snd_group1_3.play();
}
if (keyLet == 81) {
snd_g1_chnl = snd_group1_q.play();
}
if (keyLet == 87) {
snd_g1_chnl = snd_group1_w.play();
}
if (keyLet == 69) {
snd_g1_chnl = snd_group1_e.play();
}
if (keyLet == 65) {
snd_g1_chnl = snd_group1_a.play();
}
if (keyLet == 83) {
snd_g1_chnl = snd_group1_s.play();
}
if (keyLet == 68) {
snd_g1_chnl = snd_group1_d.play();
}
if (keyLet == 90) {
snd_g1_chnl = snd_group1_z.play();
}
if (keyLet == 88) {
snd_g1_chnl = snd_group1_x.play();
}
if (keyLet == 67) {
snd_g1_chnl = snd_group1_c.play();
}
}
In there, there’s nothing to stop the sounds overlapping or looping like in my other solution, also it’s obviously a lot less compact, but I sent it to my friend like that to see if it would at least play instantly. The response I got was that, although the lag was noticeably less, it was still present.
I’m unsure where to go from here, so I decided to ask for help, if anyone has a way to make sounds play the instant the key is pressed or even a way to simplify my second solution or just anything that could help, it’d be greatly appreciated.