Search code examples
actionscript-3apache-flexflex3flex4.5

Decrypt sound before playing a mp3 file in flex


This is a very broad question , can I de-crpypt a streaming file, (assuming that i encrpyting this file on the server side during upload) , in flex before it being processed for playing.


Solution

  • You can process sound bytes before playing. This is example from Adobe documentation:

    var sourceSnd:Sound = new Sound();
    var outputSnd:Sound = new Sound();
    var urlReq:URLRequest = new URLRequest("test.mp3");
    
    sourceSnd.load(urlReq);
    sourceSnd.addEventListener(Event.COMPLETE, loaded);
    
    function loaded(event:Event):void
    {
        outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
        outputSnd.play();
    }
    
    function processSound(event:SampleDataEvent):void
    {
        var bytes:ByteArray = new ByteArray();
        sourceSnd.extract(bytes, 4096);
        event.data.writeBytes(upOctave(bytes));
    }
    
    function upOctave(bytes:ByteArray):ByteArray
    {
        var returnBytes:ByteArray = new ByteArray();
        bytes.position = 0;
        while(bytes.bytesAvailable > 0)
        {
            returnBytes.writeFloat(bytes.readFloat());
            returnBytes.writeFloat(bytes.readFloat());
            if (bytes.bytesAvailable > 0)
            {
                bytes.position += 8;
            }
        }
        return returnBytes;
    }
    

    Refer this link.