Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

audioManager
Saturday, June 30th, 2007 at 4:35:38pm MDT 

  1. // ActionScript file
  2. package
  3.  
  4. {
  5.        
  6. import flash.display.Shape;
  7. import flash.display.Sprite;
  8. import flash.media.Sound;
  9. import flash.media.SoundChannel;
  10. import flash.media.SoundMixer;
  11. import flash.media.SoundTransform;
  12. import flash.utils.*;
  13. import flash.net.URLRequest;
  14. import flash.text.TextField;
  15. import flash.text.TextFormat;
  16. import flash.events.*;
  17. import flash.geom.*;
  18. import flash.filters.*;
  19. import flash.errors.IOError;
  20. import mx.controls.Alert;
  21. import mx.controls.ComboBox;
  22.  
  23.         public class audioManager extends Sprite
  24.  
  25.         {
  26.  
  27.                 private var _barArray: Array;
  28.                 private var _lineArray: Array;
  29.                 private var _sound: Sound;
  30.                 private var _songIndex: Number;
  31.                 private var _id3Index: Number;
  32.                 private var _sc: SoundChannel;
  33.                 private var _ba: ByteArray;
  34.                 private var _numBars: int;
  35.                 private var isplaying: int;
  36.                 private var _playtime: int;
  37.                 private var id3Array: Array;
  38.                 private var songArray: Array;
  39.                
  40.                 public function audioManager() 
  41.                 {
  42.        
  43.                 // the song array contains your mp3 files. Change them to reflect the mp3 you wish to play
  44.                 // to add more mp3 files add another array indice eg., songArray[3] = "somesong.mp3";
  45.                 songArray = new Array();
  46.                 songArray[0] = "assets/Darude - Ecstacy.mp3";
  47.                 songArray[1] = "assets/Darude - SandStorm.mp3";
  48.                 _songIndex = 0;
  49.                 _barArray = new Array();
  50.                 _lineArray = new Array();
  51.                 _sc = new SoundChannel();
  52.                 _sound = new Sound();
  53.                 _sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  54.                 _ba = new ByteArray();
  55.                 _playtime = 0;
  56.                 // change the number of bars
  57.                 _numBars = 60;
  58.                 createSpectrumBars();
  59.                 _sound.load(new URLRequest(songArray[_songIndex]));
  60.                 isplaying = 0;
  61.                 _id3Index = 0;
  62.                 _sc = _sound.play(_playtime, 25);
  63.                 var voltransform: SoundTransform = _sc.soundTransform;
  64.                 // set your volume for the mp3
  65.                 voltransform.volume = 0.6;
  66.                 _sc.soundTransform = voltransform;
  67.                 addEventListener(Event.ENTER_FRAME, animateBars);
  68.                 addEventListener(Event.ENTER_FRAME, getInfo);
  69.                 }
  70.        
  71.                 private function createSpectrumBars(): void     
  72.                 {       
  73.                         for (var i: int = 0; i < _numBars; i++)
  74.                         {
  75.                                 // this function allows you to change the the appearance of the bars
  76.                                 var bar: Shape = new Shape();   
  77.                                 var dropShadow: DropShadowFilter = new DropShadowFilter(6, 25, 0x00FF00, 1, 10, 10, 2, 1);     
  78.                                 bar.graphics.beginFill(0xFF0099);       
  79.                                 bar.graphics.drawRect(250, - 75, 1, 30);       
  80.                                 bar.graphics.endFill()
  81.                                 bar.filters = [dropShadow];     
  82.                                 bar.y = bar.height + 130;       
  83.                                 bar.x = 1+i * 4;       
  84.                                 addChild(bar)
  85.                                 _barArray.push(bar);   
  86.                                 // this function allows you to change the the appearance of the blue dots       
  87.                                 var line1: Shape = new Shape()
  88.                                 line1.graphics.beginFill(0x66FFFF);     
  89.                                 line1.graphics.drawRect(250, - 90, 1, 2);       
  90.                                 line1.graphics.endFill();       
  91.                                 line1.y = bar.height + 95;     
  92.                                 line1.x = 1+i * 4;
  93.                                 addChild(line1);       
  94.                                 _lineArray.push(line1)
  95.                         }       
  96.                 }
  97.                        
  98.                 private function ioErrorHandler(event: IOErrorEvent): void{
  99.        
  100.                 }
  101.        
  102.                 public function onStop():void
  103.                
  104.                 {
  105.                
  106.                 _playtime = 0;
  107.                
  108.                 _sc.stop();
  109.                
  110.                 isplaying = 1;
  111.                
  112.                 }
  113.        
  114.                 public function onPlay():void
  115.                
  116.                 {
  117.                
  118.                         if (isplaying == 0)
  119.                
  120.                         {
  121.                
  122.                         // do nothing
  123.                
  124.                         }
  125.                
  126.                         else
  127.                
  128.                         {
  129.                
  130.                         _sc = _sound.play(_playtime, 25);
  131.                
  132.                         isplaying = 0;
  133.                
  134.                         }
  135.                
  136.                 }
  137.                
  138.                 public function onPause():void
  139.                 {       
  140.                         _playtime = _sc.position;
  141.                         _sc.stop();     
  142.                         isplaying = 1;
  143.                 }
  144.                
  145.                 private var textField: TextField = new TextField();
  146.                
  147.                 private function getInfo(event: Event): void
  148.                
  149.                 {
  150.                
  151.                 // stores the MP3 ID3 info into an array, you can add more id3 info by adding another id3Array
  152.                
  153.                 id3Array = new Array();
  154.                
  155.                 id3Array[0] = "Song: " + _sound.id3.songName + " ";
  156.                
  157.                 id3Array[1] = "Artist: " + _sound.id3.artist + " ";
  158.                
  159.                 id3Array[2] = "Album: " + _sound.id3.album + " ";
  160.                
  161.                 if (_sound.id3.songName != null)
  162.                
  163.                 {
  164.                
  165.                 // formats the display and appearance of the id3 array
  166.                
  167.                
  168.                
  169.                 textField.width = 190;
  170.                
  171.                 textField.height = 20;
  172.                
  173.                 textField.x = 50;
  174.                
  175.                 textField.y = 100;
  176.                
  177.                 addChild(textField);
  178.                
  179.                 textField.text = id3Array[_id3Index];
  180.                
  181.                 var format: TextFormat = new TextFormat();
  182.                
  183.                 format.font = "Cambria";
  184.                
  185.                 format.color = 0x0099CC;
  186.                
  187.                 format.size = 13;
  188.                
  189.                 textField.setTextFormat(format)
  190.                
  191.                 }
  192.                
  193.                 id3Timer.addEventListener("timer", cycleid3Array);
  194.                
  195.                 id3Timer.start();
  196.                
  197.                 }
  198.                
  199.                 private var id3Timer: Timer = new Timer(8000, 10);
  200.                
  201.                 private function cycleid3Array(event: TimerEvent): void
  202.                
  203.                 {
  204.                
  205.                 if (_id3Index != 2) // set to higher number if you use more id3 information in the arrays.
  206.                
  207.                 {
  208.                
  209.                 _id3Index++
  210.                
  211.                 }
  212.                
  213.                 else if (_id3Index != 0)
  214.                
  215.                 {
  216.                
  217.                 _id3Index = 0;
  218.                
  219.                 }
  220.                
  221.                 }
  222.                
  223.                
  224.                
  225.                 public function cyclesongArray(dir:String,cBox:ComboBox):void
  226.                
  227.                 {
  228.                
  229.                         if (dir == 'forw' && _songIndex != 1) // set to higher number if you use more mp3 files in the songArray.       
  230.                         {
  231.                
  232.                                 _songIndex++;
  233.                                 _sc.stop();     
  234.                                 _sound = new Sound();   
  235.                                 _sound.load(new URLRequest(songArray[_songIndex]));     
  236.                                 _sc = _sound.play(_playtime, 25);       
  237.                                 _sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler)
  238.                                 var data:Number = _songIndex;   
  239.                                 data++ 
  240.                                 cBox.selectedIndex = data       
  241.                         }else if (dir == 'rev' && _songIndex != 0)     
  242.                         {       
  243.                                 _songIndex--;   
  244.                                 _sc.stop();     
  245.                                 _sound = new Sound();   
  246.                                 _sound.load(new URLRequest(songArray[_songIndex]));     
  247.                                 _sc = _sound.play(_playtime, 25);       
  248.                                 _songIndex = 0
  249.                                 var data1:Number = _songIndex; 
  250.                                 data1++ 
  251.                                 cBox.selectedIndex = data1     
  252.                         }
  253.                
  254.                 }
  255.                
  256.                 public function setSong(data:Number):void
  257.                 {
  258.                         _songIndex = data;
  259.                
  260.                         _sc.stop();
  261.                
  262.                         _sound = new Sound();
  263.                        
  264.                         if(songArray[_songIndex] != "")
  265.                         {
  266.                
  267.                         _sound.load(new URLRequest(songArray[_songIndex]));
  268.                
  269.                         _sc = _sound.play(_playtime, 25);
  270.                
  271.                         _sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  272.                         //data++
  273.                        
  274.                         //Box.selectedIndex = data
  275.                        
  276.                         }else{
  277.                                 mx.controls.Alert.show("Song doesn't exist!");
  278.                         }
  279.                 }
  280.                
  281.                 private function animateBars(event: Event): void
  282.                
  283.                 {
  284.                
  285.                         SoundMixer.computeSpectrum(_ba, true, 0);
  286.                
  287.                         for (var i: int = 0; i < _numBars; i++) 
  288.                         {
  289.                
  290.                                 _barArray[i].scaleY = _ba.readFloat();
  291.                
  292.                                 _lineArray[i].scaleY = _ba.readFloat();
  293.                
  294.                         }
  295.                
  296.                 }
  297.        
  298.         }
  299.  
  300. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right worth-right fantasy-obligation