在 C# WP7 中查找 SoundEffectInstance 的基础 SoundEffect

发布于 2024-11-17 01:51:33 字数 344 浏览 4 评论 0原文

当播放太多声音时,WP7 会遭受相当严重的性能损失。我有一个 AudioManager 来跟踪 SoundEffectInstances 并防止同时播放太多。

然而,有些东西不需要实例,只需调用 SoundEffect 本身的 Play() 即可。例如,子弹击中某物时会发出撞击声,但实际上并不需要实例。

我当前的系统仅管理实例。我想做的是看看我手头上是否有一个未使用的 SoundEffect 实例,然后简单地播放它。这需要找到 SoundEffectInstance 的 SoundEffect 类型,检查它是否已播放,如果没有则播放。这样做将使我能够更准确地跟踪总的演奏声音。

希望这是有道理的,有人能指出我正确的方向。

WP7 suffers pretty severe performance penalties when too many sounds are played. I have an AudioManager that keeps track of SoundEffectInstances and prevents too many from playing at once.

However, some things don't require an instance and simply call Play() on a SoundEffect itself. For example, a bullet plays an impact sound when it hits something but doesn't actually need an instance.

My current system only manages Instances. What I'd like to do is see if I have an existing instance of that SoundEffect on hand that's not used and simply play it. This would require finding the SoundEffect type of a SoundEffectInstance, checking if it's played and then playing it if not. Doing this will enable me to keep track of total playing sounds more accurately.

Hopefully that makes sense and someone can point me in the right direction.

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

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

发布评论

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

评论(1

一瞬间的火花 2024-11-24 01:51:33

需要决定的重要事情是如何管理同时播放的 SoundEffectInstances 数量?显而易见的方法是修改您的游戏代码(或级别或其他),以便播放声音的数量永远不会变得太多。

但如果这是不可接受的,您将需要以某种方式强制执行硬限制。问题是:你打算怎么做?每个声音效果是否有该效果的最大实例数?或者你会有“声音课程”(该课程有最大播放次数)?或者只是全局最大声音数?是否会通过阻止播放其他声音来强制执行上限?或者通过切断已经播放的声音?

我建议围绕 SoundEffect 创建一个包装对象,它实现的功能与 SoundEffect.Play 几乎相同(它在内部管理一个 SoundEffectInstance 池) code>s,玩完后重用它们)。然后在其上添加播放上限功能。

因此,您的包装类可能看起来像这样:

class MySoundEffect
{
    SoundEffect sound;
    List<SoundEffectInstance> instances;
    int maxInstances;
}

或者可能像这样:

class MySoundEffect
{
    static int[] soundClassMaximums = { 2, 2, 5, 10 };
    static int[] soundClassInstanceCount;

    SoundEffect sound;
    List<SoundEffectInstance> instances;
    int class;
}

然后您将在通常使用 SoundEffect 的任何地方使用 MySoundEffect

看看 ExEn。因为它实现了 SoundEffect 的内部结构,所以您可以借用它的池化代码,这与真正的 XNA SoundEffect 的做法类似,然后将其用作您自己的实例池以及您添加的功能。

还有其他方法可以做到这一点,这取决于您的架构。但基本上由您来跟踪声音效果和实例之间的关联。在 XNA API 中没有办法做到这一点(除了做一些讨厌的反射)。

The important thing to decide is how you are going to manage the number of playing SoundEffectInstances at once? The obvious way is to modify your gameplay code (or levels or whatever) such that the number of playing sounds simply never grows too large.

But if this is not acceptable you will need to enforce a hard limit somehow. And the question is: how are you going to do it? Will each sound effect have a maximum number of instances of that effect? Or will you have "sound classes" (where the class has a maximum play count)? Or just a global maximum sound count? Will the cap be enforced by preventing additional sounds from playing? Or by cutting off sounds that are already playing?

What I would suggest is creating a wrapper object around SoundEffect that implements pretty much the same thing that SoundEffect.Play does (it internally manages a pool of SoundEffectInstances, reusing them when they are finished playing). Then add your playback cap functionality on top of it.

So your wrapper class might look something like this:

class MySoundEffect
{
    SoundEffect sound;
    List<SoundEffectInstance> instances;
    int maxInstances;
}

Or perhaps like this:

class MySoundEffect
{
    static int[] soundClassMaximums = { 2, 2, 5, 10 };
    static int[] soundClassInstanceCount;

    SoundEffect sound;
    List<SoundEffectInstance> instances;
    int class;
}

And then you would use MySoundEffect everywhere you'd normally be using SoundEffect.

Take a look at ExEn. Because it implements the internals of SoundEffect, you can possibly borrow its pooling code, which is similar to what the real XNA SoundEffect does, and then use it as a starting point for your own instance pool with your added functionality.

There are other ways to do it, and it will depend on your architecture. But basically it is up to you to track the association between sound effects and instances. There's no way to do it in the XNA API (short of doing some nasty reflection).

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