2 Replies Latest reply on Aug 24, 2009 8:48 PM by _gilado

    how to play sound from a proprietary format

    _gilado

      I would like to convert a small java applet to flex as3

       

      The current applet connects  to a remote server to receive audio in a proprietry compressed and encrypted format using a tcp connection (the data is encoded as text)

       

      It then decrypts, decompresses the audio and uses java sound to play it.

       

      Looking at the as3 classes all I could find is a sound object that seems to only play mp3 audio and a urlrequest object that is used to download (what about streaming) mp3 audio from a server.

       

      I am looking for a simple sample that will incorporate retreving data from a server (perhaps using the http service) into e.g. a byte array, and playing pcm audio from a byte array.

       

      Ideally it would be a small set of files (1?) that I can compile using mxmlc; I am not good with IDEs, linux command line is my thing

       

      Thanks

        • 1. Re: how to play sound from a proprietary format
          msakrejda Level 4

          You could take a look at the Flash Player 10 Sound API (an example here: http://flexmonkey.blogspot.com/2008/12/born-to-synthesise-sound-synthesis-in.html ). This essentially lets you shove raw bytes into the sound output buffer. I'm not sure how well this will work for your use case (and it's Flash Player 10+ only), but it might be worth a shot...

           

          --
          Maciek Sakrejda
          Truviso, Inc.
          www.truviso.com
          Truviso is hiring: http://www.truviso.com/company-careers.php?i=87

          1 person found this helpful
          • 2. Re: how to play sound from a proprietary format
            _gilado Level 1

            Maciek, many thanks!

             

            Here's sample code showing how to stream audio in arbitrary format and play it


            // File: Player.as

            // When running from local storage (during developement)
            // compile mxmlc -use-network=false Player.as
            // otherwise loading the local raw audio file fails with error 2148
            // The input audio format is 16 bit little endian 44.1Khz mono.
            package
            {
                import flash.display.Sprite;
                import flash.text.TextField;
                import flash.media.Sound;
                import flash.media.SoundChannel;
                import flash.events.Event;
                import flash.events.SampleDataEvent;
                import flash.net.URLRequest;
                import flash.net.URLStream;


                public class Player extends Sprite
                {
                    private var sourceUri:String = "audio.44.raw";
                    private var sound:Sound = new Sound();
                    private var soundChannel:SoundChannel = new SoundChannel();
                    private var playing:Boolean;
                    private var stream:URLStream;
                    private var request:URLRequest;
                    private var statusLine:TextField = new TextField();

             

                    public function Player()
                    {
                        statusLine.width = 400;
                        statusLine.text = "Starting...";
                        addChild(statusLine);
                        getAudio();
                        start();
                    }

             

                    public function getAudio():void
                    {
                        stream = new URLStream();
                        request = new URLRequest(sourceUri);
                        try {
                            stream.load(request);
                        } catch (error:Error) {
                            statusLine.text = "Failed to load " + request.url + " : " + err!
                        }
                    }

             

                    private function playCallback(event:SampleDataEvent):void
                    {
                        var value:Number;
                        var i:int, cnt:int;

             

                        // Input is 16bit PCM, each sample occupies 2 bytes
                        cnt = stream.bytesAvailable / 2;
                        if (cnt >= 8192 || playing) {
                            playing = true;
                            statusLine.text = request.url + " : playing...";
                            if (cnt > 8192)
                                cnt = 8192;
                            for (i = 0; i < cnt; i++) {
                                // Convert 16bit little edndian pcm samples to numbers
                                value = stream.readUnsignedByte() + 256 * stream.readUnsign!
                                if (value > 32767)
                                    value -= 65536;
                                value /= 10000; //Adjust the volume
                                // Input is mono, write to both channels
                                event.data.writeFloat(value);
                                event.data.writeFloat(value);
                            }
                            if (cnt < 2048)
                                statusLine.text = request.url + " : done...";              !
                        }
                        else {
                            // Wait until enough data is bufferred
                            statusLine.text = request.url + " : buffering...";
                            for (i = 0; i < 8192; i++) {
                                event.data.writeFloat(0);
                                event.data.writeFloat(0);
                            }
                        }
                    }

             

                    private function start():void
                    {
                        soundChannel.stop();
                        sound.removeEventListener(SampleDataEvent.SAMPLE_DATA,playCallback);
                        playing = false;
                        sound.addEventListener(SampleDataEvent.SAMPLE_DATA,playCallback);
                        soundChannel = sound.play();
                    }
                }
            }