Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 3.35 KB  |  hits: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. AS3 playback of sound byte array doesn't begin at the start
  2. var soundBA:ByteArray = new ByteArray();
  3. var sound:Sound = new Sound();
  4. var ch:SoundChannel = new SoundChannel();
  5. var recordingsArray:Array = new Array();
  6.        
  7. soundBA.clear();
  8. soundBA.length = 0;
  9.  
  10. //I collect the recorded byteArray within an array
  11. soundBA.writeBytes(recordingsArray[0]);
  12.  
  13. soundBA.position = 0;
  14. trace("Start POS "+soundBA.position); //traces 0
  15.  
  16. sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sound_sampleDataHandler, false, 0, true);
  17. ch=sound.play();
  18. this.addEventListener(Event.ENTER_FRAME, updateSeek, false, 0, true);
  19.  
  20.  
  21.  
  22. public function updateSeek(event:Event):void {
  23.  
  24.     trace("current Pos "+soundBA.position); //the first trace event is "current Pos 163840"
  25. }
  26.  
  27.  
  28.  
  29. function sound_sampleDataHandler(event:SampleDataEvent):void {
  30.  
  31.     for (var i:int = 0; i < 8192; i++)
  32.     {
  33.         if (soundBA.bytesAvailable < 4)
  34.         {
  35.             break;
  36.         }
  37.         var sample:Number = soundBA.readFloat();
  38.         event.data.writeFloat(sample);
  39.         event.data.writeFloat(sample);
  40.  
  41.     }
  42.  
  43. }
  44.        
  45. public function updateSeek(event:Event):void {
  46.     trace("current pos in ms: " + ch.position);
  47.     trace("current pos in bytes: " + (ch.position * 44.1 * 4 * 2));
  48.     trace("current pos in %: " + (100 * ch.position / sound.length));
  49. }
  50.        
  51. package
  52. {
  53.     import flash.display.Sprite;
  54.     import flash.events.Event;
  55.     import flash.events.SampleDataEvent;
  56.     import flash.media.Sound;
  57.     import flash.media.SoundChannel;
  58.     import flash.net.URLRequest;
  59.     import flash.utils.ByteArray;
  60.  
  61.     public class SoundTest extends Sprite
  62.     {
  63.         private var soundSrc:Sound;
  64.         private var soundPlayer:Sound;
  65.         private var soundData:ByteArray;
  66.         private var soundChannel:SoundChannel;
  67.  
  68.         public function SoundTest()
  69.         {
  70.             soundSrc = new Sound();
  71.             soundSrc.addEventListener(Event.COMPLETE, startPlayback);
  72.             soundSrc.load(new URLRequest("sound.mp3"));
  73.         }
  74.  
  75.         private function startPlayback(e:Event = null):void
  76.         {
  77.             soundData = new ByteArray();
  78.             soundSrc.extract(soundData, soundSrc.length * 44.1, 0);
  79.             soundData.position = 0;
  80.  
  81.             soundPlayer = new Sound();
  82.             soundPlayer.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
  83.             soundChannel = soundPlayer.play();
  84.             addEventListener(Event.ENTER_FRAME, updateTime);
  85.         }
  86.  
  87.         private function onSampleData(e:SampleDataEvent):void
  88.         {
  89.             for (var i:int = 0; i < 8192; i++)
  90.             {
  91.                 if (soundData.bytesAvailable < 4)
  92.                 {
  93.                     break;
  94.                 }
  95.                 var sampleL:Number = soundData.readFloat();
  96.                 var sampleR:Number = soundData.readFloat();
  97.                 e.data.writeFloat(sampleL);
  98.                 e.data.writeFloat(sampleR);
  99.  
  100.             }
  101.         }
  102.  
  103.         private function updateTime(e:Event):void
  104.         {
  105.             trace("current pos in ms: " + soundChannel.position);
  106.             trace("current pos in bytes: " + (soundChannel.position * 44.1 * 4 * 2));
  107.             trace("current pos in % (method 1): " + (100 * soundChannel.position / soundSrc.length));
  108.             // it also works
  109.             trace("current pos in % (method 2): " + (100 * soundChannel.position / (soundData.length / (44.1 * 4 * 2))));
  110.         }
  111.     }
  112. }