使用 Silverlight 2 进行短音频缓存
我正在尝试在 Silverlight 2 中创建的游戏中使用大量短声音样本。这些样本的长度不到 2 秒。
我更愿意在初始化期间将所有音频样本加载到画布上。 我一直在画布中添加媒体元素和一个通用列表来管理它。 到目前为止,它似乎有效。
当我第一次播放样本时,它播放得非常完美。 如果它已经播放完毕并且我想重新使用相同的元素,它会切断声音的第一部分。 为了再次播放示例,我停止并播放媒体元素。
我是否应该使用另一种方法来使用样本,以便音频不会被剪辑并获得良好的性能?
I'm attempting to use a large number of short sound samples in a game I'm creating in Silverlight 2. The samples are less than 2 seconds long.
I would prefer to load all the audio samples onto the canvas during the initualization. I have been adding the media element to the canvas and a generic list to manage it. So far, it appears to work.
When I play the sample the first time, it plays perfectly. If it has finished playing and I want to re-use the same element, it cuts off the first part of the sound. To play the sample again, I stop and play the media element.
Is there another method I should use the samples so that the audio is not clipped and good performance is obtained?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此外,确保所有音频样本最初都传送到客户端可能是个好主意。 根据您的设置方式,MediaElements 可能正在使用其渐进式下载功能从服务器获取媒体文件。 虽然这本身没有任何问题(浏览器缓存应该在初始下载后帮助您解决问题),但这确实意味着您必须处理浏览器缓存,并且存在一些潜在的问题。
可以尝试的步骤:
哈特哈,
埃里克
Also, it's probably a good idea to make sure that all of your audio samples are brought down to the client side initially. Depending on how you set it up, it's possible that the MediaElements are using their progressive download functionality to get the media files from the server. While there's nothing wrong with this per se (browser caching should be helping you out after the initial download), it does mean that you have to deal with the browser cache, and there are some potential issues there.
Possible steps to try:
HTH,
Erik
一些评论:
来自 MSDN:
尝试限制应用程序中同时拥有的 MediaElement 对象的数量。 如果应用程序树中有超过一百个 MediaElement 对象,无论它们是否同时播放,都可能引发 MediaFailed 事件。 解决此问题的方法是在需要时将 MediaElement 对象添加到树中,并在不需要时将其删除。
您可以尝试找到示例的开头以重置当前正在播放的点,然后再重新使用它:
另请参阅 MSDN MediaElement.Position。
Some comments:
From MSDN:
Try to limit the number of MediaElement objects you have in your application at once. If you have over one hundred MediaElement objects in your application tree, regardless of whether they are playing concurrently or not, MediaFailed events may be raised. The way to work around this is to add MediaElement objects to the tree as they are needed and remove them when they are not.
You could try to seek to the start of the sample to reset the point currently being played before re-using it with:
See also MSDNs MediaElement.Position.
您可以使用的一种技术(尽管我不确定它在 Silverlight 中的效果如何)是创建一个大文件,将所有样本连接在一起(每个样本之间可能有半秒左右的沉默)。 找出每个样本的时间码,并寻找该位置的媒体元素并播放。 您只需要与要同时播放的声音一样多的媒体元素。
One techique you can use, although I'm not sure how well it will work in Silverlight, is create one large file with all of your samples joined together (probably with a half-second or so of silence between each). Figure out the timecode for each sample and seek the media element to that position and play. You'll only need as many media elements as simultaneous sounds you want to play.