1

说清楚:我试图通过查看此论坛中的其他帖子来找到解决方案,但没有效果(可能是因为我对 c# 编程的知识仍然薄弱)。

我目前正在开发一款 RTS 游戏。就像游戏一样——它们会发出声音。

目前,我可以使用以下代码在按下按钮时同时播放声音:

using System.Runtime.InteropServices;

int a = 1;

    [DllImport("winmm.dll")]
    static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);

private void button4_Click(object sender, EventArgs e)
    {
        mciSendString(@"open 2.wav type waveaudio alias sound2" + a, null, 0, IntPtr.Zero);
        mciSendString(@"play sound2" + a, null, 0, IntPtr.Zero);
       // mciSendString("close sound2" + a, null, 0, IntPtr.Zero);
        a+=1;
    }

它有效,但仅当每次别名都与以前不同时(这就是我在名称中使用递增数字的原因)。但是..如果将播放游戏期间的声音,假设播放 10 000 次.. 它会“吃掉”很多内存吗?

代码应该改吗?有没有“更好”的选择来做到这一点?我一直在寻找在播放完成后关闭它的条件代码,但是..我还想让声音在声音后 0.2 秒播放(取决于使用的声音,它长 2-10 秒) - 所以会有回声。我看到了一些带有 Dispose 字的代码,但我不知道如何使用它。

当我写这篇文章时,我想我想出了一个可能的解决方案:

        mciSendString("open 1.wav type waveaudio alias sound1" + a, null, 0, IntPtr.Zero);
        mciSendString("play sound1" +a, null, 0, IntPtr.Zero);
        mciSendString("close sound1" +(a-1), null, 0, IntPtr.Zero);
        a+=1;

但第一次播放的声音在第二次播放之前停止。

有没有更“专业”的解决方案来做到这一点?或者这很好?

我希望我清楚地描述了我的问题。

4

0 回答 0