#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32

    Stop all sounds for all Flash SWF embedded on a page - AS3 & AS2


    I have 100 swf embedded in a page. The swf are various forms of individual audio playbacks.

    eg.
    - an swf button that loads an swf into it that has audio on a timeline streamed. AS3
    - swf that has streamed audio using scripting so there is no preloading. AS2

    Since I have 100 and the user could press all 50 at a time and have this giant noise of sounds and bog down their bandwidth. Is there anyway of:
    - stopping all playing of swfs that has streamed audio using timeline. I guess that could be like a ALL stop frame command??
    - stop all sounds coming from the streamed audio swf

    The issues here is that they are all individual swf at different scripting of AS2 and AS3 so I am not sure if it is at all possible.

    Thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    3
    Rep Power
    0
    Hmm. That's an interesting question. I'm gonna try out some stuff and see what I get. Can you include a few audio swfs?
  4. #3
  5. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    Hi, I've done this before for a music selling website. You can use the LocalConnection object to do that. You create one clip that acts as the master listener. All the other clips register themselves with the master. A simple example case is that each of the 100 clips is a play/stop toggle button. When you press one it should call the master listener and say "I want to play". The master listener then broadcasts to all the other listeners to say "You should stop playing now". You don't have to bother with the buttons telling the master "I want to stop playing now" since that has no relevance to the other buttons.
    Quis custodiet ipsos custodes?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    3
    Rep Power
    0
    Originally Posted by Tann San
    Hi, I've done this before for a music selling website. You can use the LocalConnection object to do that[/url]. You create one clip that acts as the master listener. All the other clips register themselves with the master. A simple example case is that each of the 100 clips is a play/stop toggle button. When you press one it should call the master listener and say "I want to play". The master listener then broadcasts to all the other listeners to say "You should stop playing now". You don't have to bother with the buttons telling the master "I want to stop playing now" since that has no relevance to the other buttons.
    LocalConnection would work if you can recompile all the swfs with a snippet in each to listen for the LocalConnection call.

    @rePete do you have all the FLA's for the swf's in question?
    If no, this might require an outside the box solution.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32
    Cmndo, O.K. the files are too big for this post so I placed it here:

    files here
    Last edited by rePete; January 24th, 2012 at 04:43 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32
    Tann,

    I am giving that localConnection a try.

    Will this work though on 100 SWFs like this?

    I have audio1.swf, audio2.swf and audio3.swf
    I place this code in each swf

    Code:
    var conn:LocalConnection;
    conn = new LocalConnection();
    conn.connect(‘stopAudio_connect’);
    
    function stopAllAudio():void{
    sound_mc.stop();
    }
    
    // script to stop audio from other swf and play the audio in this swf
    var conn:LocalConnection;
    conn = new LocalConnection();
    
    btn.addEventListener(MouseEvent.CLICK, playAudio);
    
    function playAudio(e:MouseEvent):void{
    conn.send(‘stopAudio_connect’, ‘stopAllAudio’);
    sound_mc.play();
    }
    If I click the play btn on audio1.swf, then click on the play btn on audio2.swf. Will this stop audio1.swf and play audio2.swf or will it stop both and audio2.swf will not play because it stops itself?

    I just dont know if this will work on 100 swfs and I may be doing this wrong.
  12. #7
  13. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    It's been quite a while since I used the LC object, like well over a year but I can tell you roughly what I remember the logic being.

    The master clip should be a seperate SWF file, not one of the play buttons. It doesn't need to be visible. I made it to be a 10x10 clip that was absolutely positioned off the screen using CSS.

    In the master clip you create a local connection object and connect as you have in the first 3 lines. In that clip create a variable "currently_playing", I'll explain why in a bit.

    It's easier if we just take one play button to use as an example. Inside the play button you should call that master clip using the initial connection name, in your case "stopAudio_connect". The play button(s) also need to listen for the stop message from the server so each play button will require its own unique ID string to use in its connect call.

    Here is what I mean for the mater so far:

    Code:
    var conn:LocalConnection;
    conn = new LocalConnection();
    
    // The master listens on the "stopAudio_connect" channel.
    conn.connect("stopAudio_connect");
    
    function stopAllAudio():void
       {
       }
    This is the code for the play button so far:

    Code:
    var conn:LocalConnection;
    conn = new LocalConnection();
    
    // The master listens on the uniqueID channel, that being whatever the unique ID is.
    conn.connect(this.uniqueID);
    
    function stopAudio():void
       {
       }
    So now the play button can talk to the server using "stopAudio_connect" and also receive commands from the server via its unique ID.

    Now we have:
    • A master clip that each of the play buttons can talk to directly.
    • The master clip can talk to each play button by sending to that buttons unique ID.

    So whenever a play button talks to the master it should also send it's ID so the master knows who was calling:

    conn.send("stopAudio_connect", "stopAllAudio", this.uniqueID);

    Then you modify the stopAllAudio function in the master so it accepts the ID:

    Code:
    function stopAllAudio(client_id:String):void
       {
          // Tell the last play button that was playing to stop
          if(this.currently_playing != "")
             conn.send(this.currently_playing, "stopAudio");
    
          // Update the currently_playing variable with the ID of the button that called the master
          this.currently_playing = client_id;
       }
    I just made up the "currently_playing" variable which lets you keep track of the last play button to call the server. Think about it, the last button to call the servers stopAllAudio button was doing so because it was about to start playing. So therefore we know which was the last button to play and is the only one we need to stop.

    So the stopAudio function in the play button (client) might look like this:

    Code:
    function stopAudio():void
       {
          sound_mc.stop();
       }
    You could use a GUID generating ActionScript class like this one to create the unique ID. It shows the classes usage at the bottom, so a modified version to use in the play button would look like:

    Code:
    var uniqueID:String = GUID.create();
    
    // traces Unique ID: 6EF3BDEC-5B51-3CE9-429C-F99A46EC0980
    trace("Unique ID: "+ uniqueID);
    Quis custodiet ipsos custodes?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32
    I cant seem to get it to communicate with each other??
    I have made a dynamic text field called currently_playing_field to see what swf is being played on the master.swf. It doesnt change when I click a play button.

    On my master.swf
    Code:
    import flash.sampler.NewObjectSample;
    
    var currently_playing:String = "start";
    
    var conn:LocalConnection;
    conn = new LocalConnection();
    
    conn.connect("stopAudio_connect");
    
    function stopAllAudio(client_id:String):void
    {
    	conn.send(this.currently_playing, "stopAudio");
    	this.currently_playing = client_id;
    	// display what swf is playing
    	this.currently_playing_field.text = client_id;
    }
    On my play.swf
    I have given each play swf its own unique id manually
    Code:
    var conn:LocalConnection;
    conn = new LocalConnection();
    var uniqueID:String = "sample1";
    
    conn.connect(this.uniqueID);
    
    function stopAudio():void{
    	sound_mc.stop();
    }
    
    btn_sound.addEventListener(MouseEvent.CLICK, btnClick);
    
    function btnClick(e:MouseEvent):void{
    			conn.send("stopAudio_connect", "stopAllAudio", this.uniqueID);
    			sound_mc.play();
    		}
    I am also getting this error when I publish the play.swf
    Error #2044: Unhandled StatusEvent:. level=error, code=
    Last edited by rePete; January 27th, 2012 at 07:14 AM.
  16. #9
  17. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    I don't know why you would get that error when publishing. It usually means that you tried to send but there was an error. You have to listen for the STATUS event. See here.

    According to the localConnection livedocs page the status event is only ever fired when the send command is called. In the code you posted that only happens in the function called when the button is clicked.

    If the error happens only after you click the play button then it would indicate that it can't connect to the "stopAudio_connect" channel. In which case make sure you have run that SWF before clicking the play button.

    There is a whole bunch of security related things to take into account as well. It's easy if both the master and play files are on the same domain. I'm just wondering incase you have the master running in a web page but you are running the play button in the Flash IDE. They would both have different domains and as default will not be able to talk to each other. An easy way to test that is to just run both SWF files locally.
    Quis custodiet ipsos custodes?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32
    Originally Posted by Tann San
    I don't know why you would get that error when publishing. It usually means that you tried to send but there was an error. You have to listen for the STATUS event. See here.

    According to the localConnection livedocs page the status event is only ever fired when the send command is called. In the code you posted that only happens in the function called when the button is clicked.

    If the error happens only after you click the play button then it would indicate that it can't connect to the "stopAudio_connect" channel. In which case make sure you have run that SWF before clicking the play button.

    There is a whole bunch of security related things to take into account as well. It's easy if both the master and play files are on the same domain. I'm just wondering incase you have the master running in a web page but you are running the play button in the Flash IDE. They would both have different domains and as default will not be able to talk to each other. An easy way to test that is to just run both SWF files locally.
    I actually have the play.swf, loading into a console.swf.
    eg.
    play.swf is loaded into a holder_mc that is inside the console.swf
    I have 100 console.swf files that load their own individual play.swf of various audio.

    I was considering putting the connect script and that mentioned onto the console.swf but I can not seem to trigger a function from the play.swf to a function written in the console.swf.

    Its a long story, but I have to do it this way and I can not try any other method.

    Both master.swf and console.swf are in the web page. when you click the button in the console.swf, it loads the play.swf into the console.swf.
  20. #11
  21. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    An AS2 file can't directly call a function in an AS3 file and vica versa. Adobe actually recommend using the localConnection object to get the two talking. I'm not sure what is going wrong, logically it seems like it should be working so it's time to do some debugging. Can you send me the file(s) and I'll take a look, curiosity has gotten the best of me again ^_^
    Quis custodiet ipsos custodes?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32
    Originally Posted by Tann San
    An AS2 file can't directly call a function in an AS3 file and vica versa. Adobe actually recommend using the localConnection object to get the two talking. I'm not sure what is going wrong, logically it seems like it should be working so it's time to do some debugging. Can you send me the file(s) and I'll take a look, curiosity has gotten the best of me again ^_^
    Hey Tann,

    Its all done in AS3.

    O.K. Il organise it. : )

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo