# Problem In Sound Controlling Code: It Causes Strange Behavior

**URL:** <https://forum.kirupa.com/t/problem-in-sound-controlling-code-it-causes-strange-behavior/282985>\
**Category:** flash\
**Created:** [March 1, 2009, 12:14am UTC](https://forum.kirupa.com/t/problem-in-sound-controlling-code-it-causes-strange-behavior/282985 "2009-03-01T00:14:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paradise\_Wolf](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Paradise\_Wolf](https://forum.kirupa.com/u/Paradise_Wolf)\
**Post date:** [March 1, 2009, 12:14am UTC](https://forum.kirupa.com/t/problem-in-sound-controlling-code-it-causes-strange-behavior/282985/1 "2009-03-01T00:14:08Z")

</div>

[FONT=Arial]I created two simple prototype programs ( see below ) to test an algorithm of sound control in a loaded SWF before adding it in more complex programs.[/FONT]  
[FONT=Arial]Both use separated .as files. My intention is to load subsequent random external SWFs to replace what exists before in the more complex program.[/FONT]

[FONT=Arial]The problem is that the Toggling operation behaves in a weird way. It switches OFF in the first time, ON in the second time but fails to switch OFF again in the third time.[/FONT]

[FONT=Arial]What seems to be the problem ?[/FONT]

[COLOR=red][FONT=Arial]MAIN SWF:[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]public class MainMovieControlsLoadedSwfSound extends Sprite [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]{[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] private var loader:Loader;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]private var swf:Object;[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] public function MainMovieControlsLoadedSwfSound():void [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] loader = new Loader();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] loader.load(new URLRequest(’ ExternalSWFSound.swf’));[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] btn.addEventListener(MouseEvent.CLICK, SoundSwitch);[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] private function onLoadComplete(e:Event):void [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] var loaderInfo:LoaderInfo = e.target as LoaderInfo;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]addChild(loaderInfo.content);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] swf = loaderInfo.content;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]swf.x = 75;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] swf.y = 50;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] private function SoundSwitch(event:MouseEvent):void [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]swf.SoundOnOff();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[FONT=Arial] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [/FONT]

[COLOR=red][FONT=Arial]EXTERNAL SWF:[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]public class ExternalSWFSound extends Sprite [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]{[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] private var sound1:MusicaRilassante; // Contained in the SWF’s internal Library and with a linkage class[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] private var channel:SoundChannel;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]private var Toggle:Boolean = true; [/FONT][/COLOR]

[COLOR=blue][FONT=Arial] public function ExternalSWFSound():void [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]sound1 = new MusicaRilassante();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]channel= sound1.play();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] public function SoundOnOff()[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]if( Toggle == true )[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] channel.stop();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] Toggle = false;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] }[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] else[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] sound1.play();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] Toggle = true;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] }[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]
