WP7 SoundEffect 音量减小

发布于 2024-12-03 12:07:15 字数 930 浏览 0 评论 0原文

我无法弄清楚这一点。如果我单击一个文本框,它会设置一个计时器,每分钟触发 x 次。计时器似乎工作正常,但每分钟的周期数越低,声音影响播放的音量就越小。我一定不明白 XNA 框架是如何工作的。

private System.Threading.Timer tmrMetronome_m;

private void tbTempo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    TextBlock tb = (TextBlock)sender;
    int iBeatsPerMinute = int.Parse(tb.Text);

    int iMS = 1000 * 60 / iBeatsPerMinute;

    if (this.tmrMetronome_m != null)
    {
        this.tmrMetronome_m.Change(new TimeSpan(0), new TimeSpan(0, 0, 0, 0, iMS));
    }
    else
    {
        this.tmrMetronome_m = new System.Threading.Timer(MetronomeTick, null, new TimeSpan(0), new TimeSpan(0, 0, 0, 0, iMS));
    }
}

private void MetronomeTick(object state)
{
    using (System.IO.Stream strWAV = TitleContainer.OpenStream("wav/Beat.wav"))
    {
        SoundEffect effect = SoundEffect.FromStream(strWAV);
        FrameworkDispatcher.Update();
        effect.Play();
    }
}

I can't figure this one out. If I click on a textbox, it sets up a timer to fire x number of times a minute. The timer seems to work fine, but the lower the cycles per minute, the lower the volume that the sound affect plays. I must not be understanding how the XNA framework works.

private System.Threading.Timer tmrMetronome_m;

private void tbTempo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    TextBlock tb = (TextBlock)sender;
    int iBeatsPerMinute = int.Parse(tb.Text);

    int iMS = 1000 * 60 / iBeatsPerMinute;

    if (this.tmrMetronome_m != null)
    {
        this.tmrMetronome_m.Change(new TimeSpan(0), new TimeSpan(0, 0, 0, 0, iMS));
    }
    else
    {
        this.tmrMetronome_m = new System.Threading.Timer(MetronomeTick, null, new TimeSpan(0), new TimeSpan(0, 0, 0, 0, iMS));
    }
}

private void MetronomeTick(object state)
{
    using (System.IO.Stream strWAV = TitleContainer.OpenStream("wav/Beat.wav"))
    {
        SoundEffect effect = SoundEffect.FromStream(strWAV);
        FrameworkDispatcher.Update();
        effect.Play();
    }
}

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

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

发布评论

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

评论(2

2024-12-10 12:07:15

您确定这里没有其他问题吗?节拍是否可能会重叠,以便在出现频率更高时产生更大声的印象?

我在一个快速的 XNA 项目中重新创建了几乎与您完全相同的代码,并且没有遇到响度问题。显然,我并没有使用相同的音频样本。

Are you sure there isn't some other issue at play here? Could the beats possibly be overlapping to create the impression of being louder when they occur more often?

I recreated pretty much your exact code above in a quick XNA project, and did not experience the loudness issue. I was not -- obviously -- using the same audio sample though.

不乱于心 2024-12-10 12:07:15

您可能想尝试将 Stream 读出移动到某个地方的初始化。在应用程序加载期间将音效内容存储到 SoundEffect 变量中,然后在 MetronomeTick 期间调用此变量的 Play()。

根据您的需要,另一种方法是仅存储 SoundEffectInstance 变量,然后在加载时加载它,如下所示:

using (System.IO.Stream strWAV = TitleContainer.OpenStream("wav/Beat.wav"))
{
    SoundEffect effect = SoundEffect.FromStream(strWAV);
    soundInstance = effect.CreateInstance();
}

要播放它,只需在 SoundEffectInstance 对象上调用 Play() 即可。

当然,因为您只保留一个实例,所以其中两个声音永远不会重叠(不确定您想要什么)。无论哪种方式,如果这个问题仍然存在,都值得尝试。

You may want to try moving that Stream reading out to the initialization somewhere. Store the sound effect contents into a SoundEffect variable during your app loading time and then call Play() on this variable during the MetronomeTick.

Depending on your wants, an alternative is to only store a SoundEffectInstance variable, and then during load time, load it up like so:

using (System.IO.Stream strWAV = TitleContainer.OpenStream("wav/Beat.wav"))
{
    SoundEffect effect = SoundEffect.FromStream(strWAV);
    soundInstance = effect.CreateInstance();
}

To play it, just call Play() on the SoundEffectInstance object.

Of course, because you're only keeping one instance, two of these sounds will never overlap (not sure of what you want). Either way it's worth trying if this problem still persists.

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