简体   繁体   English

AS3:如何访问和控制外部SWF中的嵌入式声音?

[英]AS3: How to access and control embedded sounds in an external swf?

I rarely use sounds in AS3/Flash. 我很少在AS3 / Flash中使用声音。 I am using Flash Pro CS6, but I can't seem to figure out how to access, control (play, stop, etc) sounds embedded in an external SWF loaded into the main SWF. 我正在使用Flash Pro CS6,但似乎无法弄清楚如何访问,控制(播放,停止等)嵌入在主SWF中的外部SWF中嵌入的声音。

It's easy to control them when embedded on the main swf. 将它们嵌入主SWF时很容易控制它们。 However, on an externally loaded SWR, I get all kinds of errors. 但是,在外部加载的SWR上,我会遇到各种错误。 For this app, I really need to embed them in the external SWF. 对于此应用程序,我确实需要将它们嵌入外部SWF中。

I read several solutions, but none seem to work. 我阅读了几种解决方案,但似乎都没有用。

I embed the sound an mp3 file called soundSegment1.mp3 using Flash CS6 import tool and then open the actionscript properties panel on flash to select the class name: SoundSegment1. 我使用Flash CS6导入工具将声音嵌入了一个名为soundSegment1.mp3的mp3文件,然后打开Flash上​​的actionscript属性面板以选择类名称:SoundSegment1。 Then I edit the class code and create a file called SoundSegment1.as and it's saved right next to my document class main.as in the same directory. 然后,我编辑类代码并创建一个名为SoundSegment1.as的文件,并将其保存在我的文档类main.as旁边的同一目录中。 The code of the SoundSegment1 class looks like this: 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();
        }
    }
}

Then, in my main.as, I have done several attempts to play this sound such as: 然后,在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.

}

I get: 我得到:

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

or, I also read that I should be able to do something like this anywhere in the Main.as file: 或者,我还读到我应该能够在Main.as文件中的任何位置执行以下操作:

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

I also tried on the completeListener: 我还尝试了completeListener:

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

I get: 我得到:

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

I am stuck and I really need to get this to work. 我被困住了,我真的需要使它起作用。 I would be genuinely grateful for your help. 衷心感谢您的帮助。

Thank you in advance for any help provided. 预先感谢您提供的任何帮助。 I really need to get it to work, because I can't simply embed them in the main SWF or load them individually externally one by one. 我真的需要使它正常工作,因为我不能简单地将它们嵌入主SWF中,也不能从一个外部一个接一个地单独加载它们。

Thanks again! 再次感谢!

user SnickelFritz from Kirupa gave me a fabulous answer with much easier code, which can be very powerful when using multiple sounds. 来自Kirupa的用户SnickelFritz通过简单得多的代码给了我一个神话般的答案,当使用多种声音时,它会非常强大。 This is just great, because you can use only one preloader per SWF, instead of having multiple loader for each file: 这非常好,因为每个SWF只能使用一个预加载器,而不是每个文件具有多个加载器:

code for the main.as file main.as文件的代码

http://www.kirupa.com/forum/showthread.php?305098-Playing-a-embedded-sound-in-an-external-swf 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();

        }
    }
}

Code for the loaded swf "child.as" 加载的瑞士法郎“ 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();
        }
    }
}

User "kglad.com" on the Adobe Forums also gave me a good answer: Adobe论坛上的用户“ kglad.com”也给了我一个很好的答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM