0

我有一个分解成多个帧的 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()

我所有的按钮和影片剪辑都有实例名称。

4

1 回答 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();
于 2019-12-14T21:11:20.487 回答