Mp3Player.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import flash.media.SoundTransform;
  2. class Mp3Player extends flash.events.EventDispatcher, implements IPlayer {
  3. var File : flash.net.URLStream;
  4. var sound : flash.media.Sound;
  5. var channel : flash.media.SoundChannel;
  6. var pitch : Array<Float>;
  7. var buffer : Array<Array<Float>>;
  8. var padding : Array<Float>;
  9. var in_off : Array<Float>;
  10. var fname : String;
  11. var first : Bool;
  12. var trigger : Null<Float>;
  13. var pos : Null<Float>;
  14. var playTimer : flash.utils.Timer;
  15. var schtr: SoundTransform;
  16. public var volume(get_volume, set_volume): Float;
  17. public var pan(get_pan, set_pan): Float;
  18. public var soundTransform(get_soundTransform, set_soundTransform): SoundTransform;
  19. public function new(?path : String) {
  20. super();
  21. schtr = new SoundTransform();
  22. fname = path;
  23. File = null;
  24. }
  25. public function play(?path : String, ?trigger_buffer : Float) {
  26. // Now we don't use trigger_buffer variable anyhow. May be we don't need it
  27. if (path != null) fname = path;
  28. if (fname == null) throw "No sound URL given";
  29. trace("Mp3Player for " + fname);
  30. try {
  31. File = new flash.net.URLStream();
  32. var Req = new flash.net.URLRequest(fname);
  33. sound = new flash.media.Sound();
  34. sound.addEventListener(flash.events.IOErrorEvent.IO_ERROR, ioErrorHandler);
  35. sound.load(Req);
  36. dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, 0));
  37. }
  38. catch (error : Dynamic) {
  39. trace("Unable to load: " + error);
  40. throw error;
  41. }
  42. if (sound != null)
  43. {
  44. // Add the event listeners for load progress and load
  45. // complete
  46. sound.addEventListener(flash.events.ProgressEvent.PROGRESS, progressHandler);
  47. sound.addEventListener(flash.events.Event.COMPLETE, completeHandler);
  48. // If there's a channel
  49. if (channel != null)
  50. {
  51. channel.stop();
  52. // Play the music
  53. channel = sound.play(0);
  54. }
  55. else {
  56. // Play the music
  57. channel = sound.play(0);
  58. if (this.schtr != null) {
  59. this.channel.soundTransform = this.schtr;
  60. }
  61. // Add the event listener for sound complete
  62. channel.addEventListener(flash.events.Event.SOUND_COMPLETE, stoppedEvent);
  63. // Start a timer to show play progress, there's no
  64. // play progress event
  65. startPlayTimer();
  66. }
  67. }
  68. }
  69. public function set_volume(volume: Float): Float {
  70. this.schtr.volume = volume;
  71. trace("mp3 set_volume(" + volume + ")");
  72. this.soundTransform = this.soundTransform; // Apply changes
  73. return volume;
  74. }
  75. public function get_volume(): Float {
  76. return this.schtr.volume;
  77. }
  78. public function set_pan(pan: Float): Float {
  79. this.schtr.pan = pan;
  80. this.soundTransform = this.soundTransform; // Apply changes
  81. return this.schtr.pan;
  82. }
  83. public function get_pan(): Float {
  84. return this.schtr.pan;
  85. }
  86. public function set_soundTransform(st: SoundTransform): SoundTransform {
  87. this.schtr = st;
  88. if (this.channel != null) {
  89. this.channel.soundTransform = this.schtr;
  90. }
  91. return this.schtr;
  92. }
  93. public function get_soundTransform(): SoundTransform {
  94. return this.schtr;
  95. }
  96. function startPlayTimer() {
  97. if (playTimer != null)
  98. playTimer.stop();
  99. // Timer for emulating Playing event
  100. playTimer = new flash.utils.Timer(100, Math.round(sound.length / 100));
  101. playTimer.addEventListener(flash.events.TimerEvent.TIMER, playingEvent);
  102. playTimer.start();
  103. }
  104. function playingEvent(event: flash.events.Event) {
  105. if (channel != null) {
  106. pos = channel.position;
  107. }
  108. dispatchEvent(new PlayerEvent(PlayerEvent.PLAYING, pos));
  109. }
  110. function stoppedEvent(event: flash.events.Event) {
  111. dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, pos));
  112. }
  113. public function pause() {
  114. if (channel != null)
  115. {
  116. pos = channel.position;
  117. channel.stop();
  118. trace("mp3 Paused pos = " + pos);
  119. dispatchEvent(new PlayerEvent(PlayerEvent.PAUSED, pos));
  120. if (playTimer != null)
  121. playTimer.stop();
  122. }
  123. }
  124. public function resume() {
  125. trace("mp3 Try to resume from " + pos);
  126. channel.stop();
  127. if (pos != null) {
  128. channel = sound.play(pos);
  129. }
  130. else play();
  131. }
  132. public function seek(pos: Float) {
  133. channel.stop();
  134. channel = sound.play(pos);
  135. dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, pos));
  136. }
  137. public function stop() {
  138. trace("mp3 Stopped position = " + channel.position);
  139. pos = channel.position;
  140. channel.stop();
  141. if (File != null) {
  142. File.close();
  143. File = null;
  144. dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, 0.0));
  145. }
  146. if (playTimer != null) {
  147. playTimer = null;
  148. }
  149. }
  150. function completeHandler(event: flash.events.Event) {
  151. trace("mp3 completeHandler: " + event);
  152. dispatchEvent( new PlayerLoadEvent(PlayerLoadEvent.LOAD, false, false, sound.length, sound.length) );
  153. dispatchEvent(event);
  154. }
  155. function progressHandler(event: flash.events.ProgressEvent) {
  156. trace("mp3 progressHandler: " + event);
  157. dispatchEvent(event); // here we fire byte progress
  158. // dirty hack but could work correct I suppose :)
  159. var percent = event.bytesLoaded / event.bytesTotal;
  160. dispatchEvent( new PlayerLoadEvent(PlayerLoadEvent.LOAD, false, false, sound.length * percent, sound.length) );
  161. }
  162. function ioErrorHandler(event: flash.events.IOErrorEvent) {
  163. trace("mp3 ERROR ERROR");
  164. dispatchEvent(event);
  165. }
  166. }