Skip to content Skip to sidebar Skip to footer

Actionscript3 Calculator Sound Playing

I'm trying to make a math app using as3 in air for android. I have many frames with some stuff like stop(); QuestText.text = '0+2' Button1.LabelText.text = '6';

Solution 1:

I won't use the word "easier". But more algorithmic, yes, it is possible. You'll actually be thrilled to learn that official Adobe documentation has an example how to play a sequence of sounds. Lets adapt it to your needs.

First, you need to devise a sound cache so that you could get a sound by character value.

package
{
    import flash.media.Sound;
    import flash.events.Event;

    publicclassSoundCache{
        staticprivatevar loadingPlan:int;
        staticprivatevar completeHandler:Function;
        staticprivatevar hash:Object = newObject;

        // This method will return a Sound object by character value.staticpublicfunctionfind(name:String):Sound{
            return hash[name];
        }

        staticpublicfunctionstartEngine(handler:Function):void{
            loadingPlan = 12;
            completeHandler = handler;

            for (var i:int = 0; i < 10; i++)
                loadSound(i + "", i + "");

            loadSound("+", "plus");
            loadSound("-", "minus");
        }

        staticprivatefunctionloadSound(name:String, fileName:String):void{
            var aSound:Sound = new Sound;

            hash[name] = aSound;

            aSound.addEventListener(Event.COMPLETE, onLoaded);
            aSound.load(FilePath + "speech/" + fileName + ".mp3");
        }

        staticprivatefunctiononLoaded(e:Event):void{
            var aSound:Sound = e.target as Sound;

            aSound.removeEventListener(Event.COMPLETE, onLoaded);

            loadingPlan--;

            if (loadingPlan == 0)
            {
                if (completeHandler != null)
                {
                    completeHandler();
                    completeHandler = null;
                }
            }
        }
    }
}

So at the start of your application you call:

import SoundCache;

SoundCache.startEngine(onCache);

function onCache():void
{
    trace("All sounds are loaded!");
}

When all the sounds are loaded and ready, you can use them for playing sound sequences. Lets devise a sequence player.

package
{
    importSoundCache;

    import flash.events.Event;

    import flash.media.Sound;
    import flash.media.SoundChannel;

    publicclassSequencePlayer
    {
        // A list of active sequence players. If you don't keep// their references while they are playing,// Garbage Collector might go and get them.staticprivatevarlist:Array = newArray;

        staticpublicfunctionplay(sequence:String):void
        {
            // Sanity check.if (!sequence)
            {
                trace("There's nothing to play!");
                return;
            }

            varaSeq:SequencePlayer = newSequencePlayer;

            list.push(aSeq);

            aSeq.sequence = sequence;
            aSeq.playNext();
        }

        // *** NON-STATIC PART *** //privatevarsequence:String;
        privatevarchannel:SoundChannel;

        privatefunctionplayNext():void
        {
            varaChar:String;
            varaSound:Sound;

            // While there are still characters in the sequence,// search for sound by the first character.// If there's no such sound - repeat.while (sequence)
            {
                // Get a sound from the cache by single character.
                aChar = sequence.charAt(0);
                aSound = SoundCache.find(aChar);

                // Remove the first character from the sequence.
                sequence = sequence.substr(1);

                // Stop searching if there is a valid next sound.if (aSound) break;
            }

            if (aSound)
            {
                // If there is a valid Sound object to play, then play it// and subscribe for the relevant event to move to the// next sound when this one is done playing.
                channel = aSound.play();
                channel.addEventListener(Event.SOUND_COMPLETE, onSound);
            }
            else
            {
                // If sequence is finished and there's no valid// sound to play, remove this sequence player// from the keepalive list and forget about it.varanIndex:int = list.indexOf(this);
                if (anIndex > -1) list.splice(anIndex, 1);
            }
        }

        // Event.SOUND_COMPLETE handler.privatefunctiononSound(e:Event):void
        {
            // Sanity checks.if (!channel) return;
            if (e.target != channel) return;

            // Release the current SoundChannel object.
            channel.removeEventListener(Event.SOUND_COMPLETE, onSound);
            channel = null;

            // Move on to the next sound if any.playNext();
        }
    }
}

Though it all might look frightening so far, we're almost done. At this point, all you need to do to play a random sequence of sounds is to call the SequencePlayer.play(...) method. All the horrors above drills your problem down to this:

import SequencePlayer;

// It will ignore the space charaters so it's fine.
SequencePlayer.play(" 5 + 0 ");

Please keep in mind that though I tried to provide you with well-documented and working solution, I never actually tested it so forgive me a random typo if any.

Post a Comment for "Actionscript3 Calculator Sound Playing"