使用 Silverlight 2 进行短音频缓存

发布于 2024-07-22 11:55:48 字数 276 浏览 5 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(3

茶底世界 2024-07-29 11:55:48

此外,确保所有音频样本最初都传送到客户端可能是个好主意。 根据您的设置方式,MediaElements 可能正在使用其渐进式下载功能从服务器获取媒体文件。 虽然这本身没有任何问题(浏览器缓存应该在初始下载后帮助您解决问题),但这确实意味着您必须处理浏览器缓存,并且存在一些潜在的问题。

可以尝试的步骤:

  1. 将您的音频文件标记为“内容”。 这将使它们聚集在 .xap 中。
  2. 将音频文件加载到 MemoryStreams 中(请参阅 Application.GetResourceStream 方法)并调用 MediaElement.SetSource()。

哈特哈,
埃里克

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:

  1. Mark your audio files as "Content". This will get them balled up in the .xap.
  2. Load your audio files into MemoryStreams (see Application.GetResourceStream method) and call MediaElement.SetSource().

HTH,
Erik

空心↖ 2024-07-29 11:55:48

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:

mediaelement.Position = new TimeSpan();

See also MSDNs MediaElement.Position.

漫漫岁月 2024-07-29 11:55:48

您可以使用的一种技术(尽管我不确定它在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文