我已经试了好几个小时了。必须有一个简单的解决方案来停止声音并在as3中卸载它。
这不是我所有的代码,但简而言之,我是在加载随机声音。我需要证明bar在功能之外,所以我可以引用其他函数作为进度条。
如何卸载声音,以便使用相同的名称加载新声音,而不会在第二次调用声音时出现错误?
这次考试我有两个扣子。一个游戏声音和一个停止声音按钮。
这是我的代码:
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]我相信我更亲近了。到目前为止非常感谢你的帮助。
发布于 2011-12-27 03:46:11
为了阻止声音播放,您必须首先告诉SoundChannel实例这样停止:
mySoundChannel.stop();一旦这样做,您就可以通过调用close方法来关闭声音实例使用的流,如下所示:
TheSound.close();而且,delete关键字在as3中很少使用,当一些方法试图访问要删除的变量时,不应该使用delete关键字。如果要释放当前分配给TheSound变量的实例,则应将其值设置为null。这样,flash将正确地垃圾收集旧的声音实例,当它找到合适的时间时不再使用它。
发布于 2014-01-05 07:43:50
您可以在函数之外初始化变量,但每次调用函数时都将其定义为一个新的声音对象。这样,它具有全局范围,并且您可以在任何时候加载一个新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()
}https://stackoverflow.com/questions/8640903
复制相似问题