title: Flash AS3 - OGG sound delayed (Adobe Alchemy library)
body: <p>I wanted to make live drums play with keyboard shortcuts. The problem was that MP3 is not good enough in this case (encoders adds random silence at beginning of the sound) and also flash itself makes delay (I tried use already buffered file with play(specified_start_point_in_miliseconds)). So my solution is OGG (this format is gapless).
  I use compiled swc file from <a href="http://labs.adobe.com/wiki/index.php/Alchemy%3aLibraries" rel="nofollow">Adobe Alchemy library</a> and <a href="http://mauft.com/2010/11/ogg-vorbis-in-flash/" rel="nofollow">simple frontend for it</a>. Everything works fine, OGG file is playing, but its still much delay - no matter if its lodeaded external or as embedded, and if its first or n time. I need a solution, how to make sound play fast equally to key press.
  Here is my very simple example code:</p>
  
  <pre><code>import flash.display.Sprite;
  import flash.events.*;
  import flash.utils.ByteArray;
  import flash.events.SampleDataEvent;
  import com.mauft.OggLibrary.OggStream;
  import com.mauft.OggLibrary.OggEmbed;
  
  public class ogg extends Sprite
  {
  
      [Embed(source="/drumssnare2.ogg",mimeType="application/octet-stream")] private var OGG_FILE:Class   //Embed Ogg file as binary stream
  
      public function ogg()
      {
      var gameinfo:Sprite=new Sprite();
      gameinfo.graphics.beginFill(0x000000,1);
      gameinfo.graphics.drawRect(0,0,240,16);
      gameinfo.graphics.endFill();
      gameinfo.addEventListener(MouseEvent.CLICK, playOGG);
      addChild(gameinfo);
      //var streamTest:OggStream=new OggStream("http://127.0.0.1/~7z/drumssnare2.ogg");
  
          var embedTest:OggEmbed=new OggEmbed((new OGG_FILE) as ByteArray)    //Create new instance of OggEmbed
  
          function playOGG():void
          {
          //var streamTest:OggStream=new OggStream("http://www.vorbis.com/music/Hydrate-Kenny_Beltrey.ogg")
  
          //streamTest.play(0);
          embedTest.play(0);
  
          }
      }
  }
  </code></pre>