【问题标题】:AS3: How to access and control embedded sounds in an external swf?AS3:如何访问和控制外部 swf 中的嵌入声音?
【发布时间】:2013-09-18 20:16:36
【问题描述】:

我很少在 AS3/Flash 中使用声音。我正在使用 Flash Pro CS6,但我似乎无法弄清楚如何访问、控制(播放、停止等)嵌入在加载到主 SWF 中的外部 SWF 中的声音。

当嵌入到主 swf 中时,很容易控制它们。但是,在外部加载的 SWR 上,我会遇到各种错误。对于这个应用程序,我真的需要将它们嵌入到外部 SWF 中。

我阅读了几种解决方案,但似乎都没有。

我使用 Flash CS6 导入工具将声音嵌入了一个名为 soundSegment1.mp3 的 mp3 文件,然后在 Flash 上打开动作脚本属性面板选择类名:SoundSegment1。然后我编辑类代码并创建一个名为 SoundSegment1.as 的文件,它保存在我的文档类 main.as 旁边的同一目录中。 SoundSegment1 类的代码如下所示:

package  {

    import flash.media.*;


    public class SoundSegment1 extends Sound 
    {

        public function SoundSegment1 () 
        {
            // no code in here
        }


        public function playSound()
        {
            var soundSegment1:Sound = new SoundSegment1(); 
            var channel:SoundChannel = soundSegment1.play();
        }
    }
}

然后,在我的 main.as 中,我做了几次尝试来播放这种声音,例如:

var fileLocation:URLRequest = new URLRequest(SWFToLoad);
loader.load(fileLocation);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);

function initListener(e:Event):void // I also placed this code on the completeListener and it didn't work
{
    loader.content.soundSegment1.playSound(); // doesn't work.

}

我明白了:

Line XXX 1061: Call to a possibly undefined method playSound through a reference with static type flash.display:DisplayObject.

或者,我还读到我应该能够在 Main.as 文件中的任何地方执行类似的操作:

var theClass:Class = Class(loader.content.getDefinitionByName("SoundSegment1"));
var theSound:theClass = new theClass();
theSound.play()  //doesn't work either.

我也尝试过 completeListener:

var TheClass:Class = e.target.applicationDomain.getDefinition("SoundSegment1") as Class;
var theSound:TheClass = new TheClass();
theSound.play()  //doesn't work either.

我明白了:

ReferenceError: Error #1065: Variable SoundSegment1 is not defined.
    at flash.system::ApplicationDomain/getDefinition()

我被困住了,我真的需要让它工作。非常感谢您的帮助。

提前感谢您提供的任何帮助。我真的需要让它工作,因为我不能简单地将它们嵌入到主 SWF 中或一个一个地从外部加载它们。

再次感谢!

【问题讨论】:

    标签: actionscript-3 flash audio embed embedding


    【解决方案1】:

    来自 Kirupa 的用户 SnickelFritz 用更简单的代码给了我一个绝妙的答案,这在使用多种声音时会非常强大。这太好了,因为每个 SWF 只能使用一个预加载器,而不是为每个文件使用多个加载器:

    main.as 文件的代码

    http://www.kirupa.com/forum/showthread.php?305098-Playing-a-embedded-sound-in-an-external-swf

    package
    {
        import flash.display.*;
        import flash.media.*;
        import flash.events.*;
        import flash.net.*;
    
        public class Main extends MovieClip
        {
            var swf:MovieClip;
    
            public function Main()
            {
                var l:Loader = new Loader();
                l.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
                l.load( new URLRequest("child.swf") );
            }
    
            private function swfLoaded(e:Event):void
            {
                swf = e.target.content as MovieClip;
                addChild(swf);
    
                swf.playSound();
    
            }
        }
    }
    

    加载的 swf "child.as" 的代码

    package
    {
        import flash.display.*;
        import flash.media.*;
        import flash.events.*;
        import flash.net.*;
    
        public class Child extends MovieClip
        {
            private var s:Sound1;
            private var sc:SoundChannel;
    
            public function Child()
            {
                s = new Sound1();
                sc = new SoundChannel();
    
    
    
    // All of the following lines are actually optional, if all your graphic  elements are already inside the swf prepared in flash pro
    
                var g:Graphics = this.graphics;
                g.beginFill(0x666666);
                g.drawRect(0,0,550,400);
                g.endFill();
            }
    
            public function playSound():void
            {
                sc = s.play();
            }
        }
    }
    

    Adobe 论坛上的用户“kglad.com”也给了我一个很好的答案:

    http://forums.adobe.com/message/5681137#5681137

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2012-03-31
      • 1970-01-01
      • 2015-08-25
      • 2023-03-14
      • 2023-03-14
      • 2018-05-07
      相关资源
      最近更新 更多