首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >as3声音类卸载声

as3声音类卸载声
EN

Stack Overflow用户
提问于 2011-12-27 03:32:05
回答 2查看 7.3K关注 0票数 2

我已经试了好几个小时了。必须有一个简单的解决方案来停止声音并在as3中卸载它。

这不是我所有的代码,但简而言之,我是在加载随机声音。我需要证明bar在功能之外,所以我可以引用其他函数作为进度条。

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

这次考试我有两个扣子。一个游戏声音和一个停止声音按钮。

这是我的代码:

代码语言:javascript
复制
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;
        }

下面是我遇到的错误:

代码语言:javascript
复制
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]

更新..。我试着停止声音,然后卸载它,如下所示

代码语言:javascript
复制
mySoundChannel.stop();
TheSound.close();

但现在我发现了一个错误:

代码语言:javascript
复制
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]

我相信我更亲近了。到目前为止非常感谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-27 03:46:11

为了阻止声音播放,您必须首先告诉SoundChannel实例这样停止:

代码语言:javascript
复制
mySoundChannel.stop();

一旦这样做,您就可以通过调用close方法来关闭声音实例使用的流,如下所示:

代码语言:javascript
复制
TheSound.close();

而且,delete关键字在as3中很少使用,当一些方法试图访问要删除的变量时,不应该使用delete关键字。如果要释放当前分配给TheSound变量的实例,则应将其值设置为null。这样,flash将正确地垃圾收集旧的声音实例,当它找到合适的时间时不再使用它。

票数 3
EN

Stack Overflow用户

发布于 2014-01-05 07:43:50

您可以在函数之外初始化变量,但每次调用函数时都将其定义为一个新的声音对象。这样,它具有全局范围,并且您可以在任何时候加载一个新URL。

代码语言:javascript
复制
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()
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8640903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档