进入下一个时如何停止循环声音 frame/Errors

How to STOP looping sound when going into next frame/Errors

我有一个 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()

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

与其弄清楚为什么它不适用于所有这些帧和时间线,我认为最好编写一个集中式声音管理器 class 来处理这些事情。

实施。请记住,我没有对此进行测试,所以请原谅我偶尔出现的错字。这一切的逻辑应该是正确的。

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();