Question

I rarely use sounds in 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.

It's easy to control them when embedded on the main swf. However, on an externally loaded SWR, I get all kinds of errors. For this app, I really need to embed them in the external 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. 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. The code of the SoundSegment1 class looks like this:

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:

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:

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:

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.

Thanks again!

Was it helpful?

Solution

user SnickelFritz from Kirupa gave me a fabulous answer with much easier code, which can be very powerful when using multiple sounds. This is just great, because you can use only one preloader per SWF, instead of having multiple loader for each file:

code for the main.as file

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"

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:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top