Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var url:String = "";
  2.  
  3. var soundFactory:Sound;
  4. var channel:SoundChannel;
  5. var oldChannel:SoundChannel;
  6.  
  7. var channelStack:Array = new Array();
  8.  
  9. var vvolume:Number = 1;
  10.  
  11. var positionTimer:Timer;
  12. var isMusicPlaying = false;
  13. var startPosition:Number = 0;
  14.  
  15. playpausebtn.addEventListener(MouseEvent.CLICK, togglePlayback);
  16. stopbtn.addEventListener(MouseEvent.CLICK, stopPlayback);
  17. volup.addEventListener(MouseEvent.CLICK, mup);
  18. voldown.addEventListener(MouseEvent.CLICK, mdn);
  19. openbtn.addEventListener(MouseEvent.CLICK, oopf);
  20.  
  21.  
  22. function oopf(e:Event):void
  23. {
  24.     isMusicPlaying = false;
  25.     url = uuurl.text.substring(0, uuurl.text.length-1);
  26.     SoundChannelExample();
  27. }
  28.  
  29. function mup(e:Event):void
  30. {
  31.     if(isMusicPlaying){
  32.         vvolume+=0.1;
  33.         var trans:SoundTransform;
  34.         trans = new SoundTransform(vvolume, 0);
  35.         channel.stop();
  36.         startPosition = channel.position;
  37.         channel = soundFactory.play(startPosition,1,trans);
  38.     }
  39. }
  40.  
  41. function mdn(e:Event):void
  42. {
  43.     if(vvolume >= 0.1 && isMusicPlaying){
  44.         vvolume-=0.1;
  45.         var trans:SoundTransform;
  46.         trans = new SoundTransform(vvolume, 0);
  47.         channel.stop();
  48.         startPosition = channel.position;
  49.         channel = soundFactory.play(startPosition,1,trans);
  50.     }
  51. }
  52.  
  53. function stopPlayback(e:Event):void
  54. {
  55.     channel.stop();
  56.     channel = soundFactory.play(0);
  57.     startPosition = channel.position;
  58.     isMusicPlaying = false;
  59.     channel.stop();
  60.    
  61.     //trace(channelStack.length);
  62.     if(channelStack.length > 0) {channel = channelStack.pop(); startPosition = channel.position; isMusicPlaying = true;}
  63. }
  64. function togglePlayback(e:Event):void
  65. {
  66.  if (isMusicPlaying)
  67.     {
  68.         startPosition = channel.position;
  69.         channel.stop();
  70.         isMusicPlaying = false;
  71.     }
  72.     else
  73.     {
  74.         channel = soundFactory.play(startPosition);
  75.         isMusicPlaying = true;
  76.     }
  77. }
  78.  
  79. function SoundChannelExample() {
  80.     var request:URLRequest = new URLRequest(url);
  81.     soundFactory = new Sound();
  82.    
  83.     soundFactory.addEventListener(Event.COMPLETE, completeHandler);
  84.     soundFactory.addEventListener(Event.ID3, id3Handler);
  85.     soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  86.     soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  87.    
  88.     soundFactory.load(request);
  89.    
  90.     if (channel!=null && !ovcheck.selected) channel.stop();
  91.     else if(ovcheck.selected) channelStack.push(channel);
  92.    
  93.     channel = soundFactory.play();
  94.     channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
  95.     channel.stop();
  96.    
  97.     positionTimer = new Timer(50);
  98.     positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
  99.     positionTimer.start();
  100. }
  101.        
  102.  
  103.  function positionTimerHandler(event:TimerEvent):void {
  104.     //trace("positionTimerHandler: " + channel.position.toFixed(2) + " VOL: " + channel.soundTransform.volume);
  105.    
  106.     timeStamp.text = "Progress: " + (channel.position / soundFactory.length*100).toFixed(2) + "% ~ "
  107.      +(channel.position/1000).toFixed(0) + " / " + (soundFactory.length/1000).toFixed(0) + " sec."
  108.      + " VOL: " + channel.soundTransform.volume;
  109. }
  110.  
  111.  function completeHandler(event:Event):void {
  112.     //trace("completeHandler: " + event);
  113. }
  114.  
  115.  function id3Handler(event:Event):void {
  116.     //trace("id3Handler: " + event);
  117. }
  118.  
  119.  function ioErrorHandler(event:Event):void {
  120.     trace("ioErrorHandler: " + event);
  121.     positionTimer.stop();      
  122. }
  123.  
  124.  function progressHandler(event:ProgressEvent):void {
  125.     //trace("progressHandler: " + event);
  126. }
  127.  
  128.  function soundCompleteHandler(event:Event):void {
  129.     trace("soundCompleteHandler: " + event);
  130.     positionTimer.stop();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement