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

Posted

技术标签:

【中文标题】进入下一帧/错误时如何停止循环声音【英文标题】:How to STOP looping sound when going into next frame/Errors 【发布时间】:2020-04-06 10:15:23 【问题描述】:

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

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

【问题讨论】:

你先做什么?移动到另一帧或停止声音? 移动到另一帧。 在继续下一帧之前尝试停止声音。在我看来,soundsoundChannel 的范围仅限于该框架。 它不工作。关于代码的某些东西也导致了 typeError,我无法弄清楚。 【参考方案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();

【讨论】:

啊,我不熟悉包文件,这就是为什么没有想到这一点。但我读了一点并实现了这个,非常感谢你!我不知道该怎么感谢你才足够。对于代码和向我介绍包。 @S.Joey 它们被称为 AS3 classes(包只是组织 classes 的一种方式),我很高兴它有所帮助。

以上是关于进入下一帧/错误时如何停止循环声音的主要内容,如果未能解决你的问题,请参考以下文章

光滑的滑块 - 当滑块进入下一帧时暂停 YouTube 剪辑

为啥这个声音循环不会停止?

使用 javax.sound.sampled.Clip 在游戏中播放、循环和停止多个声音。意外错误

当我更改 viewController 时,viewWillDisappear 错误/停止播放声音

iPhone 上的 AVAudioPlayer 声音意外停止循环:如何调试?

停止特定的setTimeout循环如何使用clearTimeout