【问题标题】:how to replay sound in actionscript 3如何在actionscript 3中重放声音
【发布时间】:2013-02-05 16:25:27
【问题描述】:

我正在尝试在我的游戏中重放这个声音...但由于某种原因它可以工作,有人可以解释一下原因吗?

   var morning:Sound =new alarmclock ();
                       var transforming:SoundTransform = new SoundTransform(0.5);
                       var morningChannel:SoundChannel = morning.play(0,0,transforming);
                       morningChannel.addEventListener(Event.SOUND_COMPLETE, replay);
                       function replay (event:Event) {
                    morningChannel = morning.play(0,0,transforming);    
                    trace ("ANYBODY IN THERE????");
                    }

【问题讨论】:

  • 你需要展示你所有的代码。 pausing 是什么?
  • alarmclock() 是什么?
  • 请也显示该课程。此外,您应该通过以大写命名您的类来遵守 AS3 约定。 IE:应该是Alarmclock(),而不是alarmclock()
  • 我在 Flash 中做,所以类名在库中我只是在这里访问它......

标签: actionscript-3 audio


【解决方案1】:

这样做:

function replay (event:Event) {
  morningChannel = morning.play(0,0,transforming);    
  trace ("ANYBODY IN THERE????");
  morningChannel.addEventListener( Event.SOUND_COMPLETE, replay );
}

[请注意,我们再次将事件侦听器添加到声音通道。这个 是因为“morningChannel = Morning.play(0,0,transforming);”这一行 导致 Sound Channel 上的所有事件侦听器丢失。]

我借用http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/的解释

【讨论】:

    【解决方案2】:

    试试这个:

    导入 flash.media.Sound;

    导入 flash.media.SoundChannel;

    导入 flash.events.Event;

    var demoSound:声音;

    var demoSoundChannel:SoundChannel;

    playSound();

    函数 playSound():void {

    demoSound = new soundObj();// soundObj is your sound.
    demoSoundChannel = new SoundChannel();
    demoSoundChannel = demoSound.play();
    demoSoundChannel.addEventListener(Event.SOUND_COMPLETE, onComplete);
    

    }

    函数 onComplete(e:Event):void {

    playSound();
    

    }

    只是一个简单的循环播放示例,它正在工作。 您可以在播放方法中添加参数,它不会影响循环。

    【讨论】:

      【解决方案3】:

      尝试:

      function replay (event:Event)
      {
          morningChannel = morning.play(pausing,1,transforming);               
      }
      

      【讨论】:

        【解决方案4】:

        尝试侦听由Sound 对象调度的Event.COMPLETE。我过去曾成功地用它来重放声音。

        我不知道SoundChannel 调度它自己的Event.SOUND_COMPLETE 事件。您的代码(以及来自@Lee Burrows 的类似代码)似乎都应该可以工作。

        无论如何,在Sound 对象上监听Event.COMPLETE 可能值得一试:

        var morningChannel:SoundChannel = morning.play(pausing,1,transforming);
        morning.addEventListener(Event.COMPLETE, replay);
        
        function replay (event:Event)
        {
            morningChannel = morning.play(pausing,1,transforming);               
        }
        

        如果这也不起作用,则可能是某些东西正在被垃圾收集。

        另外,您将1 传递给play() 方法,因此它循环一次。那行得通吗?也许这在某种程度上令人困惑。您可以尝试将其设置为 0,因为无论如何您都在重放声音。

        最后,如果它仍然不工作,你应该在replay() 方法中设置一个断点(或在其中添加一个trace() 语句),以便确定事件是否被调度。

        【讨论】:

        • 它不适用于完整的事件......我的代码循环了一次,但随后它就停止了......我尝试输入跟踪......它只被调度一次......
        • 我也摆脱了暂停只是为了看看它是否改变,但由于某种原因它只喜欢循环一次
        猜你喜欢
        • 2017-07-02
        • 1970-01-01
        • 2010-12-09
        • 2010-09-18
        • 2015-09-13
        • 2010-09-11
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        相关资源
        最近更新 更多