尝试将声音元素捕获到 Flash CS5-as3 中

发布于 2024-10-17 06:00:48 字数 447 浏览 3 评论 0原文

大家好——有人知道为什么这段代码不起作用吗?

与其他负载[示例图像]完美配合声音...不:(

mySoundURL = new URLRequest(var+".mp3"); 
mySoundURLDefault = new URLRequest("default.mp3"); 

try{ 
    sound.load(mySoundURL); 
}catch(e:IOErrorEvent){ 
    trace("Can't load sound: "+e); 
    sound.load(mySoundURLDefault);
} 

这是我收到的错误:

Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error

谢谢,美好的一天!

Hi All—anyone has any idea why this code doesn’t work?

With other load [example image] work perfect with sound...no :(

mySoundURL = new URLRequest(var+".mp3"); 
mySoundURLDefault = new URLRequest("default.mp3"); 

try{ 
    sound.load(mySoundURL); 
}catch(e:IOErrorEvent){ 
    trace("Can't load sound: "+e); 
    sound.load(mySoundURLDefault);
} 

this is the error I’m getting:

Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error

Thanks and good day!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2024-10-24 06:00:48

将 try/catch 与加载器一起使用。下面是您要做的事情:

sound.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorEvent);
sound.load(mySoundURL); 

private function onIOErrorEvent(e:IOErrorEvent):void{
  trace(e);
  trace(e.message);

  // ^  This will show you the file it tried to load and failed at

  e.currentTarget.removeEventListener(IOErrorEvent.IOError, onIOErrorEvent);
  e.currentTarget.removeEventListener(Event.COMPLETE, onComplete;
  // ^ don't forget to clean up your listeners!
}

正如您所看到的,这样做的原因是,未捕获的错误不会告诉您任何有用的信息,例如哪个声音加载失败,或者您尝试联系的 URL 是什么。

我认为@Alexander Sobolev 的第一个猜测是正确的,因为你仍然会遇到错误。处理来自加载器的错误与处理来自同步代码的错误只是不同,try/catch 在这里不会为您提供帮助。

如果您想尝试在第一个声音失败时播放替代声音,您可以在 onIOErrorEvent 函数中执行此操作。

其他时候,您会在使用 Loader 加载 swf 或图像文件

 loader.contentLoaderInfo.addEventListener(IOErrorEvent....

或 URLLoader时监听 IOErrorEvent

 urlLoader.addEventListener(IOErrorEvent...

You do not use try/catch with loaders. Here's what you do instead:

sound.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorEvent);
sound.load(mySoundURL); 

private function onIOErrorEvent(e:IOErrorEvent):void{
  trace(e);
  trace(e.message);

  // ^  This will show you the file it tried to load and failed at

  e.currentTarget.removeEventListener(IOErrorEvent.IOError, onIOErrorEvent);
  e.currentTarget.removeEventListener(Event.COMPLETE, onComplete;
  // ^ don't forget to clean up your listeners!
}

The reason for this is, as you've seen, the uncaught error does not tell you anything helpful, such as which sound failed to load, or what the URL you tried to contact was.

I think @Alexander Sobolev is on the right track with his first guess, based on the fact that you still get an error. Handling errors from loaders is just different than handling errors from synchronous code, and try/catch will not help you here.

If you want to try playing an alt sound on fail from the first one, you'd do that in the onIOErrorEvent function.

Other times you'd listen for IOErrorEvent are when using Loader to load a swf or image file

 loader.contentLoaderInfo.addEventListener(IOErrorEvent....

Or a URLLoader

 urlLoader.addEventListener(IOErrorEvent...
沩ん囻菔务 2024-10-24 06:00:48

帖子中的信息不多,我建议检查以下条件:

1)您的 var+".mp3" 是正确的 URI 吗?它是否指向现有文件?只需在浏览器中尝试一下 - 它应该找到该文件吗?
如果您指向互联网上的文件,您应该使用类似 http://site.com/song.mp3
如果您尝试播放硬盘上的文件,您应该使用直接文件地址,例如 C:/audio/song.mp3

2) 如果 URI 没问题,则文件可能不是 mp3 格式。

3)你可以尝试像这样使用SoundLoaderContext

            var mySoundURL = new URLRequest(var+".mp3")
            var context:SoundLoaderContext = new SoundLoaderContext(0, false);
            snd.load(mySoundURL , context);

It is not much information in the post, i suggest to check following conditions:

1) Is your var+".mp3" a correct URI? Does it point to the existing file? Just try in the browser - should it find the file?
If you are pointing to the file in internet you should use notation like http://site.com/song.mp3
If you are trying to play a file on your hard drive you should use direct file address like C:/audio/song.mp3

2) If its ok with the URI than maybe file is not in mp3 format.

3) You can try to play with SoundLoaderContext like

            var mySoundURL = new URLRequest(var+".mp3")
            var context:SoundLoaderContext = new SoundLoaderContext(0, false);
            snd.load(mySoundURL , context);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文