AS3 在第一个音频文件完成后播放第二个音频文件

发布于 2024-12-09 16:27:28 字数 476 浏览 2 评论 0原文

任何帮助将不胜感激。

我正在加载这样的声音:

var mySound:Sound = new Sound();
mySound.load(new URLRequest("audio/filename.mp3"));
mySoundChannel:SoundChannel = mySound.play();

然后我添加一个侦听器并尝试使用 SOUND_COMPLETE 事件调用函数,如下所示:

mySoundChannel.addEventListener(Event.SOUND_COMPLETE,audioComplete);
function audioComplete(Event:Event){
trace("done!");
}

但是,我不断收到此错误:“1046:未找到类型或不是编译-时间常数:事件。”

谁能告诉我我做错了什么?

谢谢。

Any help would be greatly appreciated.

I'm loading a sound like this:

var mySound:Sound = new Sound();
mySound.load(new URLRequest("audio/filename.mp3"));
mySoundChannel:SoundChannel = mySound.play();

Then I'm adding a listener and trying to call a function with the SOUND_COMPLETE event, like this:

mySoundChannel.addEventListener(Event.SOUND_COMPLETE,audioComplete);
function audioComplete(Event:Event){
trace("done!");
}

However, I keep getting this error: "1046:Type was not found or was not a compile-time constant:Event."

Can anyone give me a hint as to what I'm doing wrong?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请持续率性 2024-12-16 16:27:28

更改

function audioComplete(Event:Event){

function audioComplete(event:Event){

AS 区分大小写。 事件事件不同。 Event 是一个类的名称。 event 是您分配给 Event 类型的局部变量的名称。

此外,声音是异步加载到 Flash 中的。这意味着当您调用 sound.load() 时,Flash Player 将开始在新线程上加载声音,并在加载声音时继续执行下一行代码。当声音完全加载时,Event.COMPLETE 事件被触发,NOT 当声音播放完毕时被触发。

要在声音播放完毕时触发函数,请使用 setTimeout

var mySound:Sound = new Sound();
mySound.load(new URLRequest("audio/filename.mp3"));
mySound.addEventListener(Event.COMPLETE, soundLoaded);

function soundLoaded(e:Event):void {
    mySound.play();
    setTimeout(audioComplete, mySound.length);
}
function audioComplete(){
    trace("done!");
}

Change

function audioComplete(Event:Event){

to

function audioComplete(event:Event){

AS is case sensitive. event is not the same as Event. Event is the name of a class. event is the name you are assigning to the local variable of the type Event

Moreover, sound is loaded in Flash asynchronously. This means that when you call sound.load(), Flash Player will start loading the sound on a new thread and continue the next lines of code while the sound is loading. The Event.COMPLETE event is triggered when this sound is fully loaded, NOT when the sound has finished playing.

to trigger a function when a sound has finished playing, use setTimeout

var mySound:Sound = new Sound();
mySound.load(new URLRequest("audio/filename.mp3"));
mySound.addEventListener(Event.COMPLETE, soundLoaded);

function soundLoaded(e:Event):void {
    mySound.play();
    setTimeout(audioComplete, mySound.length);
}
function audioComplete(){
    trace("done!");
}
勿挽旧人 2024-12-16 16:27:28

尝试将

function audioComplete(Event:Event){

更改为

function audioComplete(e:Event){

最好不要使用类名称作为变量名称。它可能会让试图阅读代码的编译器和程序员感到困惑。

Try changing

function audioComplete(Event:Event){

to

function audioComplete(e:Event){

It's a good idea to not use class names for your variable names. It can confuse the compiler and programmers trying to read the code.

揪着可爱 2024-12-16 16:27:28

导入事件类:
导入 flash.events.Event

package
{
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    
    public class Test extends Sprite
    {
        
        private var _sound : Sound:
        private var _soundChannel : SoundChannel:

        public function Test() : void
        {
            playSound("audio/filename.mp3");
        }
    
        private function playSound(url : String) : void
        {
            // start sound here
            _sound = new Sound();
            _sound.load(new URLRequest(url));
            _soundChannel = mySound.play();

            _soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
        }

        private function onSoundComplete(event : Event) : void
        {
            // sound is complete
            _soundChannel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);

            // play next sound
        }
    }   
}

Import the Event class:
import flash.events.Event

package
{
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    
    public class Test extends Sprite
    {
        
        private var _sound : Sound:
        private var _soundChannel : SoundChannel:

        public function Test() : void
        {
            playSound("audio/filename.mp3");
        }
    
        private function playSound(url : String) : void
        {
            // start sound here
            _sound = new Sound();
            _sound.load(new URLRequest(url));
            _soundChannel = mySound.play();

            _soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
        }

        private function onSoundComplete(event : Event) : void
        {
            // sound is complete
            _soundChannel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);

            // play next sound
        }
    }   
}
鸩远一方 2024-12-16 16:27:28

感谢各位的回复。我最终找到了一种更好的方法来做到这一点。如果有人遇到同样的问题,这对我有用:

var mySound:Sound = new Sound();
var mySoundURL:URLRequest = new URLRequest("myfile.mp3");
var mySoundChannel:SoundChannel = new SoundChannel();
mySound.load(mySoundURL);
mySoundChannel = mySound.play();
mySoundChannel.addEventListener(Event.SOUND_COMPLETE, endMySound);

function endMySound(e:Event):void{
trace("sound is complete!");
}

Thanks to those of you who responded. I ended up finding a better way to do this. If anyone runs into the same problem, here is what is working for me:

var mySound:Sound = new Sound();
var mySoundURL:URLRequest = new URLRequest("myfile.mp3");
var mySoundChannel:SoundChannel = new SoundChannel();
mySound.load(mySoundURL);
mySoundChannel = mySound.play();
mySoundChannel.addEventListener(Event.SOUND_COMPLETE, endMySound);

function endMySound(e:Event):void{
trace("sound is complete!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文