Posted By

dhjapan on 05/23/08


Tagged

as mp3 player


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

snucko
SpinZ
thehamilton
tspitzr
faugustin


MP3 Player


 / Published in: ActionScript
 

  1. import er4d.util.Delegate;
  2. import er4d.transitions.Tween;
  3.  
  4. class panik.PlayerMp3 extends MovieClip
  5. {
  6.  
  7. private var play_btn:MovieClip;
  8. private var pause_btn:MovieClip;
  9. private var timeline:MovieClip;
  10. private var timeline_bg:MovieClip;
  11.  
  12. private var songTrack:Sound;
  13. private var loaded:Boolean;
  14. private var _src:String;
  15.  
  16. private var track_position:Number;
  17. private var track_duration:Number;
  18.  
  19. private var loading_int:Number;
  20. private var playing_int:Number;
  21.  
  22.  
  23. function PlayerMp3()
  24. {
  25. loaded = false;
  26. }
  27.  
  28.  
  29. // INIT
  30. public function set src(_src:String) {
  31. this._src = _src;
  32. loaded = false;
  33. }
  34. public function get isLoaded() {
  35. return loaded;
  36. }
  37. public function loadMP3 (autoplay:Boolean) {
  38. songTrack = new Sound();
  39. songTrack.onLoad = Delegate.create(this, songLoaded, autoplay);
  40. songTrack.loadSound(_src, true);
  41. loaded = true;
  42. loading_int = setInterval(this, "checkLoadingProgress", 100);
  43. // CONTROL
  44. timeline._width = 0;
  45. play_btn.onPress = Delegate.create(this, startSound);
  46. pause_btn.onPress = Delegate.create(this, stopSound);
  47. if(autoplay) enablePlayBtn (false);
  48. else enablePlayBtn (true);
  49.  
  50. }
  51. // SOUND CONTROL
  52. public function stopSound () {
  53. songTrack.stop();
  54. enablePlayBtn (true);
  55. }
  56. public function startSound (n:Number) {
  57. // play
  58. if (!n || songTrack.position == songTrack.duration) n = 0;
  59. songTrack.start(n);
  60. enablePlayBtn (false);
  61. }
  62.  
  63. public function lock() {
  64. this._x += 5000;
  65. play_btn.enabled = pause_btn.enabled = false;
  66. }
  67. public function unlock() {
  68. this._x -= 5000;
  69. play_btn.enabled = pause_btn.enabled = true;
  70. }
  71.  
  72. // PRIVATE
  73.  
  74. // watch song loading
  75. private function checkLoadingProgress () {
  76. var numBytesLoaded:Number = songTrack.getBytesLoaded();
  77. var numBytesTotal:Number = songTrack.getBytesTotal();
  78. var numPercentLoaded:Number = Math.floor(numBytesLoaded / numBytesTotal * 100);
  79. }
  80.  
  81. // song loaded
  82. private function songLoaded (autoplay:Boolean) {
  83. // play stop
  84. if(!autoplay) stopSound(); else
  85. clearInterval(loading_int);
  86. playing_int = setInterval(this, "checkPlayingProgress", 100);
  87. track_duration = songTrack.duration/1000;
  88. }
  89. private function checkPlayingProgress () {
  90. track_position = songTrack.position/1000;
  91. var track_percent:Number = track_position / track_duration * 100;
  92. if(songTrack.position == songTrack.duration) stopSound();
  93. timeline._width = timeline_bg._width * track_percent / 100;
  94. }
  95.  
  96. private function enablePlayBtn (b:Boolean){
  97. if (b) {
  98. Tween.prop(play_btn, "_alpha", 100);
  99. play_btn.enabled = true;
  100. Tween.prop(pause_btn, "_alpha", 30);
  101. pause_btn.enabled = false;
  102. } else {
  103. Tween.prop(play_btn, "_alpha", 30);
  104. play_btn.enabled = false;
  105. Tween.prop(pause_btn, "_alpha", 100);
  106. pause_btn.enabled = true;
  107. }
  108. }
  109.  
  110. }

Report this snippet  

You need to login to post a comment.