2013-04-29 58 views
0

好的,所以我正在为我正在制作的网站使用我简单的actionscript-3声音播放器......并且虽然这样做,但发生了SOUND_COMPLETE事件由于某种原因不会起火。所以,如果有人似乎注意到我的代码中的问题,请回复!Actionscript 3:Event.SOUND_COMPLETE不会触发

package { 
    import flash.events.*; 
    import flash.media.*; 
    import flash.external.*; 
    import flash.net.*; 
    import flash.utils.*; 

    public class player{ 

     private var soundChannel:SoundChannel; 
     private var sound:Sound; 
     private var lastPosition:Number = 0; 

     public function player():void{ 
      ExternalInterface.addCallback("load", this.load); 
      ExternalInterface.addCallback("play", this.play); 
      ExternalInterface.addCallback("stop", this.stop); 
      ExternalInterface.addCallback("reset", this.reset); 
     } 
     /* 
     javascript from inside actionscript: 

      ExternalInterface.call("console.log","ipsum"); 
     */ 
     private function load(url:String):void{ 
      var audio:URLRequest = new URLRequest(url); 
      try { 
       this.soundChannel.stop(); 
      } catch(e:Error) { 
      }; 
      this.sound = new Sound(); 
      this.sound.load(audio); 
      this.lastPosition = 0; 
     } 
     private function play():void{ 
      this.soundChannel = this.sound.play(this.lastPosition); 
      this.soundChannel.addEventListener(Event.SOUND_COMPLETE,finished); 
      ExternalInterface.call("console.log","started playing"); 
     } 
     private function finished():void{ 
      this.lastPosition=0; 
      this.soundChannel=this.sound.play() 
      ExternalInterface.call("console.log","finished playing"); 
     } 
     private function reset():void{ 
      this.lastPosition = 0; 
      this.soundChannel.stop(); 
     } 
     private function stop():void{ 
      try { 
       this.lastPosition = this.soundChannel.position; 
       this.soundChannel.stop(); 
      } catch(e:Error) { 
      }; 
     } 

    } 
}//package 
+0

http://www.untoldentertainment.com/blog/2009/10/14/as3 -pitfalls-sound_complete-event-is-not-firing/ – adaam 2013-04-29 20:13:30

+0

这在这种情况下不适用于我,因为每次我使用播放函数时,都会再次添加EventListener。 感谢您的回复! – 2013-04-29 20:15:27

+2

我认为你应该在加载时调用你的本地停止函数,并且我认为你在finished()函数中得到了一个运行时错误,因为它不能接受它将被传递的事件。运行时错误阻止了代码的恢复运行。我只能假设你没有看到它,因为你禁用了调试,或者没有在调试播放器中运行它。 – 2013-04-29 20:51:34

回答

0

我相信问题是finished()不接受的情况下,它应该是:

private function finished(e:Event):void{ 
     this.lastPosition=0; 
     this.soundChannel=this.sound.play() 
     ExternalInterface.call("console.log","finished playing"); 
    } 
+0

它似乎正在工作!非常感谢。 – 2013-04-30 08:12:24