我已经尝试了几个小时了。必须有一个简单的解决方案来停止声音并在 as3 中卸载它。
这不是我的全部代码,但简而言之,我正在加载随机声音。我需要将某些变量放在函数之外,以便我可以将它们与其他函数一起引用以获得进度条。
如何卸载声音,以便可以使用相同名称加载新声音,而不会在第二次调用时出现错误?
我在这个测试中有两个按钮。一个播放声音和一个停止声音按钮。
这是我的代码:
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]
更新....我尝试停止声音,然后按如下方式卸载它
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]
我相信我更接近了。非常感谢您迄今为止的帮助。
最佳答案
为了停止播放声音,您首先必须告诉 SoundChannel 实例停止,如下所示:
mySoundChannel.stop();
完成此操作后,您可以通过调用 close 方法来关闭声音实例使用的流,如下所示:
TheSound.close();
此外,delete 关键字在 as3 中很少使用,当某些方法试图访问您要删除的变量时,您不应该使用它。如果您想释放当前分配给 TheSound 变量的实例,则应将其值设置为 null。这样,当 flash 找到合适的时间时,它就会正确地对不再使用的旧 Sound 实例进行垃圾回收。
关于actionscript-3 - as3声音类卸载声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640903/