首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在进入下一帧/错误时停止循环声音

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

Stack Overflow用户
提问于 2019-12-13 00:08:21
回答 1查看 448关注 0票数 0

我有一个flash项目被分解成多个框架,每个帧上都有一个按钮来播放下一个帧。(在每一帧上播放一个电影,直到你点击下一个帧按钮)

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

在第4帧中,我有这样的代码:

代码语言:javascript
运行
复制
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();在下一帧。

但是,每当我这样做时,输出会读到:

代码语言:javascript
运行
复制
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()

我所有的按钮和电影都有实例名。

EN

Stack Overflow用户

回答已采纳

发布于 2019-12-14 21:11:20

与其弄清楚为什么它不能与所有这些框架和时间线一起工作,我认为最好是组成一个集中的声音管理器类来处理这些事情。

Implementation.请记住,我没有测试它,所以请原谅我偶尔出错。这一切的逻辑应该是正确的。

代码语言:javascript
运行
复制
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();
        }
    }
}

使用.

代码语言:javascript
运行
复制
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();
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59314454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档