Sound keeps playing after external swf was unloaded

假装没事ソ 提交于 2020-01-04 04:43:26

问题


I have a flash application, some kind of a play-list that loads external SWF video player (I don't have code access to that external file), so users can watch the video or skip to another one. When user switches to another video new SWF file is being loaded.

The problem: If user didn't finish watching the video and skips to the next then I unload previous SWF file (unloadAndStop()) and load a new one. And because the previous SWF was playing it is not actually unloaded, it is still playing on the background (I hear two audio tracks: current and previous).

I tried SoundMixer.stopAll() call, but it doesn't actually help, because of the loading on the background and and sound over the current video when it starts playing.

Is there a way to solve this from the main 'loader' application?


回答1:


Interestingly enough, unloadAndStop() was intended for your exact situation. According to the docs...

Example: A SWF file that has a music track is loaded into an application. 
Later, the SWF is unloaded using Loader.unload(). 
Though the SWF will be removed from the screen, the music will still be heard.

SOLUTION
Loader.unloadAndStop is a new addition to Action Script 3's API. 
It helps developers to correctly stop and unload loaded content 
using the Loader.load/loadBytes APIs.

...Flash Player recursively attempts to stop and clear as many 
objects as possible (Sounds, NetStreams, EventListeners, etc.) 
within the loaded SWF. This feature is especially useful 
when unloading unknown 3rd party content.

This seems to exactly fit your situation! Provided your Loader implementation is correct , this sounds like a bug in the unloadAndStop() method.

One alternative solution could be to create a new instance of the Loader as opposed to using the same loader for each videos.

private var currentMovie:DisplayObject;

private function loadSWF(url:String ):void
{   
   if( currentMovie != null )
    {
       //stop the sound playing in the current movie
       //then remove it from the display list
       removeChild( currentMovie );
       currentMovie = null;
    }

   var loader:Loader = new Loader();
   //configureListeners
   request.url = url;
   loader.load( request , context );

}

private function onLoadComplete(event:Event):void
{
   currentMovie = event.target.loader.content;
   //etc...
}



回答2:


Try stopping all sounds before playing a new sound. Check out stopAll() call.



来源:https://stackoverflow.com/questions/3930516/sound-keeps-playing-after-external-swf-was-unloaded

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