Posted By

damarev on 04/14/09


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

alptugan
tspitzr
fana


Mp3Player Class


 / Published in: ActionScript 3
 

  1. /*
  2. var player:Mp3Player = new Mp3Player();
  3. player.play('blah.mp3');
  4. //or
  5. var player:Mp3Player = new Mp3Player();
  6. player.playlist = ['item1.mp3','item2.mp3'];
  7. player.next();
  8. */
  9. package {
  10. import flash.events.Event;
  11. import flash.events.EventDispatcher;
  12. import flash.media.Sound;
  13. import flash.media.SoundChannel;
  14. import flash.media.SoundTransform;
  15. import flash.net.URLRequest;
  16. import flash.utils.clearInterval;
  17. import flash.utils.setInterval;
  18.  
  19. public class Mp3Player extends EventDispatcher {
  20.  
  21. static public const EVENT_TIME_CHANGE:String = 'Mp3Player.TimeChange';
  22. static public const EVENT_VOLUME_CHANGE:String = 'Mp3Player.VolumeChange';
  23. static public const EVENT_PAN_CHANGE:String = 'Mp3Player.PanningChange';
  24. static public const EVENT_PAUSE:String = 'Mp3Player.Pause';
  25. static public const EVENT_UNPAUSE:String = 'Mp3Player.Unpause';
  26. static public const EVENT_PLAY:String = 'Mp3Player.Play';
  27. //
  28. public var playing:Boolean;
  29. public var playlist:Array;
  30. public var currentUrl:String;
  31. public var playlistIndex:int = -1;
  32. //
  33. protected var sound:Sound;
  34. protected var soundChannel:SoundChannel;
  35. protected var soundTrans:SoundTransform;
  36. protected var progressInt:Number;
  37.  
  38. public function play( url:String ):void {
  39. clearInterval(progressInt);
  40. if ( sound ) {
  41. soundChannel.stop();
  42. }
  43. currentUrl = url;
  44. sound = new Sound();
  45. sound.addEventListener(Event.COMPLETE, onLoadSong);
  46. sound.addEventListener(Event.ID3, onId3Info);
  47.  
  48. sound.load(new URLRequest(currentUrl));
  49.  
  50. soundChannel = sound.play();
  51. if ( soundTrans ) {
  52. soundChannel.soundTransform = soundTrans;
  53. } else {
  54. soundTrans = soundChannel.soundTransform;
  55. }
  56. soundChannel.addEventListener(Event.COMPLETE, onSongEnd);
  57. playing = true;
  58. clearInterval(progressInt);
  59. progressInt = setInterval(updateProgress, 30);
  60. dispatchEvent(new Event(EVENT_PLAY));
  61. }
  62. public function pause():void {
  63. if ( soundChannel ) {
  64. soundChannel.stop();
  65. dispatchEvent(new Event(EVENT_PAUSE));
  66. playing = false;
  67. }
  68. }
  69. public function unpause():void {
  70. if ( playing ) return;
  71. if ( soundChannel.position < sound.length ) {
  72. soundChannel = sound.play(soundChannel.position);
  73. soundChannel.soundTransform = soundTrans;
  74. } else {
  75. soundChannel = sound.play();
  76. }
  77. dispatchEvent(new Event(EVENT_UNPAUSE));
  78. playing = true;
  79. }
  80. public function seek( percent:Number ):void {
  81. soundChannel.stop();
  82. soundChannel = sound.play(sound.length * percent);
  83. }
  84. public function prev():void {
  85. playlistIndex--;
  86. if ( playlistIndex < 0 ) playlistIndex = playlist.length - 1;
  87. play(playlist[playlistIndex]);
  88. }
  89. public function next():void {
  90. playlistIndex++;
  91. if ( playlistIndex == playlist.length ) playlistIndex = 0;
  92. play(playlist[playlistIndex]);
  93. }
  94. public function get volume():Number {
  95. if (!soundTrans) return 0;
  96. return soundTrans.volume;
  97. }
  98. public function set volume( n:Number ):void {
  99. if ( !soundTrans ) return;
  100. soundTrans.volume = n;
  101. soundChannel.soundTransform = soundTrans;
  102. dispatchEvent(new Event(EVENT_VOLUME_CHANGE));
  103. }
  104. public function get pan():Number {
  105. if (!soundTrans) return 0;
  106. return soundTrans.pan;
  107. }
  108. public function set pan( n:Number ):void {
  109. if ( !soundTrans ) return;
  110. soundTrans.pan = n;
  111. soundChannel.soundTransform = soundTrans;
  112. dispatchEvent(new Event(EVENT_PAN_CHANGE));
  113. }
  114. public function get length():Number {
  115. return sound.length;
  116. }
  117. public function get time():Number {
  118. return soundChannel.position;
  119. }
  120. public function get timePretty():String {
  121. var secs:Number = soundChannel.position / 1000;
  122. var mins:Number = Math.floor(secs / 60);
  123. secs = Math.floor(secs % 60);
  124. return mins + ":" + (secs < 10 ? "0" : "") + secs;
  125. }
  126. public function get timePercent():Number {
  127. if ( !sound.length ) return 0;
  128. return soundChannel.position / sound.length;
  129. }
  130. protected function onLoadSong( e:Event ):void {
  131. }
  132. protected function onSongEnd( e:Event ):void {
  133. if ( playlist )
  134. next();
  135. }
  136. protected function onId3Info( e:Event ):void {
  137. dispatchEvent(new Event(Event.ID3, e.target.id3));
  138. }
  139. protected function updateProgress():void {
  140. dispatchEvent(new Event(EVENT_TIME_CHANGE));
  141. if ( timePercent >= .99 ) {
  142. onSongEnd(new Event(Event.COMPLETE));
  143. clearInterval(progressInt);
  144. }
  145. }
  146. }
  147. }

Report this snippet  

You need to login to post a comment.