actionscript-3 - as3声音类卸载声音

标签 actionscript-3 audio flash-cs5

我已经尝试了几个小时了。必须有一个简单的解决方案来停止声音并在 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/

相关文章:

actionscript-3 - AS3 使用 Matrix 将 "scale"对象从其 "center"

python - 通过树莓派同时播放多个声音文件,最好使用python

python - 如何修复UnicodeDecodeError

javascript - Photoshop 脚本 - 更新窗口中的进度条

actionscript-3 - 调试 Flash ActionScript 3 (AS3) 的最佳工具

java - Air Native Extension 找不到 Volley java 类,NoClassDefFoundError

arrays - 如何将 json 数组转换为 flex 4 数组 (as3)?

iphone - AVAudioPlayer 有问题吗?制造延迟

actionscript-3 - Actionscript 3.0 SharedObject刷新()问题

actionscript-3 - Netstream 和 step() 或 seek()?