2011-12-27 186 views
2

我已经试过这个小时。必须有一个简单的解决方案来停止声音并将其卸载为as3。as3声音类卸载声音

这不是我所有的代码,但总之我加载了随机声音。我需要certian vars以外的功能,所以我可以引用他们与进度条的其他功能。

如何卸载声音,以便我可以使用相同的名称加载新的声音而不会在第二次调用它时发生错误?

我在这个测试中有两个按钮。播放声音和停止声音按钮。

这里是我的代码:

var TheSound:Sound = new Sound(); 
var mySoundChannel:SoundChannel = new SoundChannel(); 

PlayButton.addEventListener(MouseEvent.CLICK, PlaySound); 
StopButton.addEventListener(MouseEvent.CLICK, Stopsound); 

function PlaySound(e:MouseEvent) 
     { 
     TheSound.load(new URLRequest("http://www.MyWebsite.com/Noel.mp3")); 
     mySoundChannel = TheSound.play(0); 
     } 

function StopSound(e:MouseEvent) 
     { 
     delete TheSound; 
     } 

以下是错误我得到:

Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful. 
    at flash.media::Sound/_load() 
    at flash.media::Sound/load() 
    at Untitled_fla::MainTimeline/PlaySound()[Untitled_fla.MainTimeline::frame1:21] 

UPDATE ....我试过回采的声音,然后卖掉,如下

mySoundChannel.stop(); 
TheSound.close(); 

但现在我得到这个错误:

Error: Error #2029: This URLStream object does not have a stream opened. 
    at flash.media::Sound/close() 
    at Untitled_fla::MainTimeline/shut1()[Untitled_fla.MainTimeline::frame1:35] 

我相信我更接近。非常感谢您的帮助。

回答

3

为了阻止播放声音,你首先要告诉SoundChannel实例来阻止像这样:

mySoundChannel.stop(); 

一旦你这样做,你可以通过调用附近的声音实例所使用的流像这样的关闭方法:

TheSound.close(); 

此外,delete关键字很少在AS3使用,你不应该它,而一些方法试图访问要删除的变量。如果您想要处理当前分配给您的TheSound变量的实例,则应将其值设置为null。通过这种方式,闪存将正确地垃圾收集旧的Sound实例,该实例在找到合适的时间时不再使用。

+0

哎感谢您的快速帮助。好吧,我试过你的代码,但我得到一个新的错误。我把它贴在上面。 – 2011-12-27 03:59:20

+0

我相信它应该没问题,如果你只是删除关闭流的部分。 – joelrobichaud 2011-12-27 04:15:23

+0

好吧,它停止流,但是当我点击重新加载它时,它得到上面提到的第一个错误 “以不正确的顺序调用函数,或者更早的调用不成功。” – 2011-12-27 04:17:25

1

您可以初始化该函数以外的变量,但每次调用该函数时将其定义为新的Sound对象。这样它就具有全局范围,并且您可以随时加载新的URL。

var TheSound:Sound; 
var mySoundChannel:SoundChannel = new SoundChannel(); 

PlayButton.addEventListener(MouseEvent.CLICK, PlaySound); 
StopButton.addEventListener(MouseEvent.CLICK, StopSound); 

function PlaySound(e:MouseEvent) 
     { 
     TheSound = new Sound(); 
     TheSound.load(new URLRequest("http://www.MyWebsite.com/Noel.mp3")); 
     mySoundChannel = TheSound.play(0); 
     } 

function StopSound(e:MouseEvent) 
     { 
     mySoundChannel.stop(); 
     TheSound.close() 
     }