Share Pastebin
Guest
Public paste!

eirikhm

By: a guest | Mar 12th, 2010 | Syntax: ActionScript 3 | Size: 6.10 KB | Hits: 940 | Expires: Never
Copy text to clipboard
  1. package {
  2.         import com.greensock.TweenLite;
  3.         import com.greensock.easing.*;
  4.         import com.greensock.plugins.TweenPlugin;
  5.         import com.greensock.plugins.VolumePlugin;
  6.        
  7.         import flash.display.Sprite;
  8.         import flash.events.Event;
  9.         import flash.events.IOErrorEvent;
  10.         import flash.media.Sound;
  11.         import flash.media.SoundChannel;
  12.         import flash.media.SoundLoaderContext;
  13.         import flash.media.SoundTransform;
  14.         import flash.net.URLRequest;
  15.         import flash.system.System;
  16.         import flash.text.TextField;
  17.         import flash.text.TextFieldAutoSize;
  18.        
  19.         public class debug_radio extends Sprite
  20.         {
  21.                 private var txtOutput:TextField;
  22.                 private var txtMessages:TextField;
  23.                
  24.                 // start time
  25.                 private var startTime:Date;
  26.                
  27.                 // last stream load
  28.                 private var lastLoad:Date;
  29.                
  30.                 private var button:Sprite;
  31.                
  32.                 // initial memory usage on load
  33.                 private var initialMemory:Number;
  34.                
  35.                 private static const streamUrl:String = "http://radio.hiof.no/nrk-mpetre-128";
  36.        
  37.                 // used for loading sounds
  38.                 private var soundRequest:URLRequest;
  39.                 private var soundLoaderContext:SoundLoaderContext;             
  40.                
  41.                 // first sound + channel
  42.                 private var activeSound:Sound;
  43.                 private var activeSoundChannel:SoundChannel;
  44.                
  45.                 // second sound  + channel
  46.                 private var idleSound:Sound;
  47.                 private var idleSoundChannel:SoundChannel;             
  48.                
  49.                 // counter for channel swaps
  50.                 private var reloadCount:int = 0;
  51.                
  52.                 // buffer size in KB
  53.                 private var bufferSize:int = 400;
  54.                
  55.                 // size of current buffer
  56.                 private var currentBuffer:int = 0;
  57.                
  58.                 public function debug_radio()
  59.                 {
  60.                         TweenPlugin.activate([VolumePlugin]);
  61.                         startTime = new Date();
  62.                        
  63.                         graphics.beginFill(0xFFFFFF,1);
  64.                         graphics.drawRect(0,0,800,410);
  65.                         graphics.endFill();
  66.                                
  67.                         txtOutput = new TextField();
  68.                         txtOutput.x = 100;
  69.                         txtOutput.y = 100;
  70.                         txtOutput.autoSize = TextFieldAutoSize.LEFT;
  71.  
  72.  
  73.                         txtMessages = new TextField();
  74.                         txtMessages.x = 100;
  75.                         txtMessages.y = 100;
  76.                         txtMessages.autoSize = TextFieldAutoSize.LEFT;
  77.                                                
  78.                         addChild(txtOutput);
  79.                         addChild(txtMessages);
  80.                        
  81.                        
  82.                         addEventListener(Event.ENTER_FRAME,updateMemoryUsage);
  83.                        
  84.                         // set up request and loader context
  85.                         soundRequest = new URLRequest(streamUrl);
  86.                         soundLoaderContext = new SoundLoaderContext(3000, false);
  87.                         soundLoaderContext.bufferTime = 1;
  88.                        
  89.                        
  90.                         // set up initial stream
  91.                         activeSoundChannel= new SoundChannel();
  92.                         activeSoundChannel.soundTransform = new SoundTransform(1,0);
  93.                         activeSound = new Sound();
  94.                        
  95.                         lastLoad = new Date();
  96.                        
  97.                         initialMemory = System.totalMemory/1024;
  98.                        
  99.                         // load and play initial stream
  100.                         activeSound.load(soundRequest,soundLoaderContext);
  101.                         activeSoundChannel = activeSound.play(0,0,activeSoundChannel.soundTransform);
  102.                 }
  103.                
  104.                 private function onIoError(e:IOErrorEvent):void
  105.                 {
  106.                         trace(e.text);
  107.                 }
  108.                
  109.                 private function swapStreams():void
  110.                 {
  111.                        
  112.                         // set up secondary sound channel
  113.                         idleSoundChannel = new SoundChannel();
  114.                         idleSoundChannel.soundTransform = new SoundTransform(0,0);
  115.                        
  116.                         // set up secondary sound object, initiate load
  117.                         idleSound = new Sound();
  118.                         idleSound.load(soundRequest,soundLoaderContext);
  119.                         idleSound.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
  120.                         idleSoundChannel = idleSound.play(0,0,idleSoundChannel.soundTransform);
  121.                          
  122.                        
  123.                        
  124.                         reloadCount++;
  125.                         lastLoad = null;
  126.                         lastLoad = new Date();
  127.                        
  128.                         /**
  129.                          * 1. start loading idleStream
  130.                          */
  131.                        
  132.                         // initiate tweening of volume on both channels
  133.                         TweenLite.to(idleSoundChannel, 4, {volume:1,onComplete:onCrossFadeComplete});
  134.                         TweenLite.to(activeSoundChannel, 4, {volume:0,onComplete:onCrossFadeComplete});                
  135.                 }
  136.                
  137.                 private function onCrossFadeComplete():void
  138.                 {
  139.                         // we have reached complete cross-fade, swap channels, clear memory for old objects
  140.                         if (activeSoundChannel.soundTransform.volume <= 0 && idleSoundChannel.soundTransform.volume >= 1)
  141.                         {
  142.                                 activeSoundChannel.stop();
  143.                                 activeSound.close();
  144.                                
  145.                                 activeSoundChannel = null;
  146.                                 activeSound = null;
  147.                                
  148.                                 activeSoundChannel = idleSoundChannel;
  149.                                 activeSound = idleSound;
  150.                                
  151.                                 idleSound = null;
  152.                                 idleSoundChannel = null;
  153.                         }                      
  154.                 }
  155.                
  156.                 private function updateMemoryUsage(e:Event):void
  157.                 {
  158.                         var deltaMemory:Number = (Number(System.totalMemory/1024) - initialMemory);
  159.                        
  160.                         if (currentBuffer > bufferSize && idleSoundChannel == null)
  161.                         {
  162.                                 // buffer has reached limit, and we do not have an instance in idleSoundChannel, start the swapping
  163.                                 swapStreams();
  164.                         }
  165.                        
  166.                        
  167.                         // some usage info
  168.                         var now:Date = new Date();
  169.                         var deltaStart:Date = new Date(now.time - startTime.time);
  170.                         var deltaLoad:Date = new Date(now.time - lastLoad.time);                       
  171.                        
  172.                         var str:String = "";
  173.                        
  174.                         str += "General Info\n";
  175.                         str += "Elapsed time : " + deltaStart.hoursUTC + ":" + deltaStart.minutes + ":" + deltaStart.seconds + "\n";
  176.                         str += "Stream Size : " + Number(activeSound.bytesTotal/1024).toFixed(2) + "kb\n";
  177.                         str += "Total memory : " + Number(System.totalMemory/1024).toFixed(2) + "kb\n";                
  178.                         str += "Delta memory: " + deltaMemory.toFixed(2) + "kb\n";
  179.  
  180.                         var streamRate:Number = Number((activeSound.bytesLoaded/1024) / (deltaLoad.time/1000));
  181.  
  182.                         str += "\n\n\nStreaming info\n";
  183.                         str += "Age : " + deltaLoad.minutes + ":" + deltaLoad.seconds + "\n";                  
  184.                         str += "Bytes loaded :  " + Number(activeSound.bytesLoaded/1024).toFixed(2) + "kb\n";
  185.                         str += "Rate : " + streamRate.toFixed(2) + "kb/sec / " + Number(streamRate*60).toFixed(2) + "kb/min \n";
  186.                         str += "Remaning buffer: " + Number(bufferSize - currentBuffer).toFixed(2) + "kb\n";
  187.                         str += "Expected buffer life duration : " + Number(bufferSize/streamRate).toFixed(2) + "sec / " + Number(bufferSize/streamRate/60).toFixed(2) + "min\n" ;
  188.                         str += "Buffer life remaining : " + Number((bufferSize/streamRate) - (deltaLoad.time/1000)).toFixed(2) + "\n";                 
  189.                        
  190.                         currentBuffer = activeSound.bytesLoaded/1024;
  191.                         str += "\n\nReload count : " + reloadCount + "\n";
  192.                        
  193.                         txtOutput.text = str;
  194.                        
  195.                         deltaMemory = 0;
  196.                         deltaStart = null
  197.                         deltaLoad = null
  198.                         streamRate = 0;
  199.                         now = null;
  200.                         str = null;
  201.                 }
  202.         }
  203. }