Stopping Flash 4 SWF with Sound in Flex 2
I recently had an issue where in Flex 2 I had to load and play a Flash 4 SWF that had embedded sound in it. I'm not a Flash developer so I'm not 100% sure if the sound was encoded as an event or what have you. In any case as soon as the SWF was loaded it would start playing and it would loop. Since Flex 2 uses Flash 9 the Flash 4 SWF shows up as an AVM1Movie when loaded which means you can't do much of anything to the movie once it's loaded. Most annoyingly the sound keeps playing and playing.
Problem 1: How to stop the sound?
The only fix I found was to call SoundMixer.stopAll();. Drastic, but it worked.
Problem 2: How to stop the sound looping?
Great the sound stopped, temporarily, only problem was when the SWF looped the sound started up again. The fix requires using a Loader and a call to unload() when you are done using it:
// create a loader and load the file var loader:Loader = new Loader(); loader.load(new URLRequest("sound.swf")); // later when you want to stop it SoundMixer.stopAll(); loader.unload();
Problem 3: Security issues?
This works if you are requesting the SWF from your own server, but if you are using another site to store the sound files (say a CDN), you'll run into security sandbox issues. To get around this the remote site will need to have an appropriate crossdomain.xml file and you'll need to use a custom loader context:
// create the loader context var loaderContext:LoaderContext = new LoaderContext(); loaderContext.securityDomain = SecurityDomain.currentDomain; loaderContext.applicationDomain = ApplicationDomain.currentDomain; loaderContext.checkPolicyFile = true; // use it when loading the file loader.load(new URLRequest("sound.swf"), loaderContext);
You should also setup a listener on loader.contentLoaderInfo to register when the SWF has loaded as unload() only works when the loader has completed, prior to that you need to use close().