Unloading swf using the unloadAndStop() method, but video sounds remain audible

天涯浪子 提交于 2019-12-02 07:19:37

The appropriate way to handle this is to program a destroy function into whatever loaded content you have, and then call it before you unload that content. In the destroy function the loaded swf should be responsible for its own business in regards to...

  • Stopping playing sounds.
  • Closing any open streams (like streaming video).
  • calling stop on its timelines
  • etc...

SoundMixer.stopAll() is in no way an appropriate solution. Rather, it can be if you are the one setting the parameters of what is "acceptable" as a jarring user experience, but if you are writing a 3rd party swf that is to be loaded into someone else's application you'll most certainly be responsible for cleaning up your own sound mess, and SoundMixer will kill not just your sounds, but the sounds from the loader, and that is one sure fire way to anger your hosts.

After much research I have solved this problem. In the hope that this will make someone's life a lot easier, here was the issue:

The unloadAndStop(); method removes any event listeners, instances, objects etc from the loaded SWF. In my case I had videos, and IF the video was still playing when the user exited (and the loaded unloaded the content) the sounds would carry on. It seems that one thing the unloadAndStop(); function does NOT do is stop all sounds.

The solution? I attached an event listener to my loader, and after the content was successfully UNLOADED, I called the SoundMixer.stopAll(); function which ensured no sounds carried on playing! This DOES however mean that if you had ambient background sounds all along, they would stop too. You could go even further and get the current time of your ambient sounds and simply "restart" them immediately from that point in time.

My simple vent listener looks like this:

myloader.contentLoaderInfo.addEventListener(Event.UNLOAD, cleanUp);

function cleanUp(e:Event):void{
   SoundMixer.stopAll(); //stop all sounds...
}

I trust this will help someone! It's a little "hacky" but it does the job just fine.

Kind regards, Simon

mat

In your video.swf, you have to write something like this:

this.addEventListener(Event.REMOVED_FROM_STAGE, stopSound)

function stopSound(e:Event):void
{
    my_flvPlayer.stop();
}

The eventlistener that listens for the Event.UNLOAD and then calls the function which pulls out the 'SoundMixer.stopAll()' function is a decent workaround. This doesn't work if you are loading your swf from another domain than the one your container swf is on. I'm pretty sure it triggers a sandbox violation. I do not have a solution for that - and the best solution anyway is the one mentioned above, mentioned by scriptocalypse.

But I'm in a situation where I am on different domains AND I have no control over the SWF files - so I can't put 'destroy' functions into them. I'm going crazy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!