进入下一帧时如何停止循环播放声音/错误

问题描述 投票:0回答:1

我有一个Flash项目,分为多个帧,每个帧上都有一个按钮,可以播放下一帧。 (在播放的每一帧上都有一个动画片段,直到您按下下一帧按钮为止)

在每一帧上,我都希望音频播放并循环播放。但是,当我单击按钮转到下一帧时,我希望停止播放一帧的音频。

在第4帧,我有此代码:

import flash.media.SoundChannel;

var sound:Sound = new firt2();
var soundChannel:SoundChannel;

sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);

sound.play();

function onSoundLoadComplete(e:Event):void{
    sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}

function onSoundChannelSoundComplete(e:Event):void{
    e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);

}

并且有效。但是,一旦单击按钮转到下一帧,我想停止它。我试过了:soundChannel.stop();在下一帧。

但是,只要我这样做,输出将显示为:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at hhh4_fla::MainTimeline/frame5()
at flash.display::MovieClip/gotoAndPlay()
at hhh4_fla::MainTimeline/fl_ClickToGoToAndPlayFromFrame()

我所有的按钮和动画片段都有实例名称。

actionscript-3 adobe-animate
1个回答
1
投票

而不是弄清楚为什么它不适用于所有这些框架和时间表,我认为最好组成一个集中的声音管理器类来处理这些事情。

实施。请记住,我没有对此进行测试,因此请原谅我偶尔输入错误。这一切的逻辑应该是正确的。

package
{
    import flash.system.ApplicationDomain;

    import flash.media.SoundChannel;
    import flash.media.Sound;

    import flash.events.Event;

    public class Audio
    {
        // Container to cache Sound objects.
        static private const cache:Object = new Object;

        // Variables to hold the current values.
        static private var currentChannel:SoundChannel;
        static private var currentSound:String;

        // Stops the current sound playing. If you pass the sound name, it
        // will stop the audio track only if it is the exact one playing.
        // Otherwise it will stop any one currently playing.
        static public function stop(value:String = null):void
        {
            // Do nothing if nothing is playing right now,
            // or if the specific sound requested to stop does not match.
            if (currentSound == null) return;
            if (value) if (value != currentSound) return;

            // Unsubscribe from event and stop the audio.
            currentChannel.removeEventListener(Event.SOUND_COMPLETE, onComplete);
            currentChannel.stop();

            // Final clean-up.
            currentChannel = null;
            currentSound = null;
        }

        // Plays the embedded sound by its class name.
        static public function play(value:String):void
        {
            // Do nothing if the requested sound is already playing.
            if (value == currentSound) return;

            // Stop the current audio track playing.
            stop();

            // Check if that one sound is valid and/or was previously requested.
            if (!cache[value])
            {
                try
                {
                    // Obtain class definition from the project.
                    var aClass:Class = ApplicationDomain.currentDomain.getDefinition(value) as Class;

                    // Try instantiating the Sound.
                    if (aClass) cache[value] = new aClass as Sound;
                }
                catch (fail:Error)
                {
                    // Well, do nothing, yet.
                }
            }

            if (cache[value])
            {
                // Store the id of audio track that is going to be playing.
                currentSound = value;

                // Play the track and subscribe to it for the SOUND_COMPLETE event.
                currentChannel = (cache[value] as Sound).play();
                currentChannel.addEventListener(Event.SOUND_COMPLETE, onComplete);
            }
            else
            {
                // If there's no such class, or it is not a Sound,
                trace("ERROR: there's no sound <<" + value + ">> is embedded into the project.");
            }
        }

        // Event handler to clean up once the current audio track is complete.
        static private function onComplete(e:Event):void
        {
            // Sanity check.
            if (e.target != currentChannel) return;

            stop();
        }
    }
}

用法

import Audio;

// Any time you want different sound to play.
// Pass the class name as Sting as an argument.
Audio.play("firt2");

// Any time you just want to stop the sound;
Audio.stop();
© www.soinside.com 2019 - 2024. All rights reserved.