universal class for playing sound


/ Published in: ActionScript 3
Save to your folder(s)

this Class can play sound from url or library in one parameter


Copy this code and paste it in your HTML
  1. package decko.sound {
  2. import flash.media.*;
  3. import flash.events.*;
  4. import flash.net.URLRequest;
  5. import flash.utils.Dictionary;
  6. import flash.utils.getDefinitionByName;
  7. import flash.system.ApplicationDomain;
  8. import flash.media.Sound;
  9.  
  10. import com.greensock.TweenMax;
  11.  
  12. /**
  13. * Trieda na jednoduchu manipulaciu so zvukom. Hlavny parameter soundID v prikazoch moze nadobudat
  14. * 4 rozne datove typy (String ako url, String ako kompletnu cestu ku Class, samotny objekt Class, objekt Sound).
  15. * @author FCI
  16. * @version v2
  17. */
  18. public class SoundMix {
  19. public static var muteFade:Number = .1;
  20. protected static var _mute:Boolean;
  21. protected static var dictionary:Dictionary = new Dictionary();
  22. protected static var stUpdate:SoundTransform;
  23. public var sound:Sound;
  24. public var playing:Boolean;
  25. public var channel:SoundChannel;
  26. public var fade:Number = .5;
  27. public var parallel:Boolean = true;
  28. public var volume:Number = 1;
  29. public var loops:int = 0;
  30. public var soundComplete:Function = null;
  31.  
  32. /**
  33. * Spusti prehravanie zvuku
  34. * @param soundID tento parameter moze prijimat 4 rozne typy dat: Class vo forme retazca s kompletnym mennym priestorom, priamo ako Class, objekt Sound a url cestu k mp3 suboru
  35. * @param vars parallel: true, volume: 1, loops: 0, fade: .5, onComplete: null
  36. */
  37. public function SoundMix(soundID:Object, vars:Object = null) {
  38. if (vars == null) {
  39. vars = { parallel: 1, volume: 1, loops: 1, fade: 1, soundComplete: 1 };
  40. }
  41.  
  42. vars.hasOwnProperty("parallel") ? parallel = vars.parallel : true;
  43. vars.hasOwnProperty("volume") ? volume = vars.volume : null;
  44. vars.hasOwnProperty("loops") ? loops = vars.loops : null;
  45. vars.hasOwnProperty("fade") ? fade = vars.fade : null;
  46. vars.hasOwnProperty("onComplete") ? soundComplete = vars.onComplete : null;
  47.  
  48. if (parallel || !(dictionary[soundID] && dictionary[soundID].playing)) {
  49. if (dictionary[soundID] && dictionary[soundID].playing){
  50. TweenMax.killTweensOf(dictionary[soundID].channel);
  51. }
  52. if(soundID is String){
  53. if (!ApplicationDomain.currentDomain.hasDefinition(String(soundID))) {
  54. var reg:RegExp = /\.mp3/i;
  55. if(reg.test(String(soundID))){
  56. sound = new Sound(new URLRequest(String(soundID)));
  57. sound.addEventListener(IOErrorEvent.IO_ERROR, soundIOError)
  58. }else{
  59. throw new Error("Error url or soundID!");
  60. }
  61. } else {
  62. var soundClass:Class;
  63. soundClass = getDefinitionByName(String(soundID)) as Class;
  64. sound = new soundClass();
  65. }
  66. }else if (soundID is Class) {
  67. sound = new soundID();
  68. }else if(soundID is Sound){
  69. sound = Sound(soundID);
  70. }
  71.  
  72. channel = sound.play(0, loops, new SoundTransform(0));
  73. channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
  74.  
  75. TweenMax.to(channel, fade, {volume: volume});
  76.  
  77. playing = true;
  78. dictionary[soundID] = this;
  79. }
  80. }
  81.  
  82. private function soundIOError(e:IOErrorEvent):void {
  83. throw new Error("Error url in soundID!");
  84. }
  85.  
  86. public function soundStop():void {
  87. channel.stop();
  88. channel.removeEventListener(Event.SOUND_COMPLETE, playComplete);
  89. playing = false;
  90. }
  91.  
  92. protected function playComplete(e:Event):void {
  93. playing = false;
  94. soundComplete == null ? soundComplete() : null;
  95. }
  96.  
  97. // STATIC FUNCTION
  98. // ----------------------------------------------------------------------------------------------------------
  99.  
  100. /**
  101. * Spusti prehravanie zvuku
  102. * @param soundID tento identifikator moze prijimat 4 rozne typy dat: Class vo forme retazca s kompletnym mennym priestorom, priamo ako Class, objekt Sound a url cestu k mp3 suboru
  103. * @param vars parallel: true, volume: 1, loops: 0, fade: .5, onComplete: null
  104. */
  105. public static function play(soundID:Object, vars:Object = null):SoundMix {
  106. return new SoundMix(soundID, vars);
  107. }
  108.  
  109. /**
  110. * @param soundID identifikator zvuku
  111. */
  112. public static function stop(soundID:Object):void {
  113. if (dictionary[soundID] && dictionary[soundID].playing) {
  114. TweenMax.to(dictionary[soundID].channel, dictionary[soundID].fade, {volume: 0, onComplete: dictionary[soundID].soundStop});
  115. }
  116. }
  117.  
  118. /**
  119. * overi ci sa prave prehrava skladba s konkretnym soundID
  120. * @param soundID nazov CLASS s kompletnou cestou alebo url adresa
  121. */
  122. public static function isPlaying(soundID:Object):Boolean {
  123. return dictionary[soundID].playing;
  124. }
  125.  
  126. public static function get mute():Boolean {
  127. return _mute;
  128. }
  129.  
  130. public static function set mute(value:Boolean):void {
  131. _mute = value;
  132.  
  133. var volumeInit:Number = int(_mute);
  134. var volume:Number = int(!_mute);
  135. stUpdate = new SoundTransform(volumeInit);
  136. TweenMax.to(stUpdate, muteFade, { volume: volume, onUpdate: updateVolume } );
  137. }
  138.  
  139. protected static function updateVolume():void {
  140. SoundMixer.soundTransform = stUpdate;
  141. }
  142. }
  143. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.