0

我正在 XNA 中创建一个游戏,它将与许多音乐循环一起工作,但我似乎无法同步这些声音。

我总是错过几毫秒你能帮我吗?

这是我第一次尝试同步声音。请注意,我需要处理数十种声音......

这个同步问题可能与缓存声音有关吗?
是否有外部库使其更容易?

    public SoundEffectInstance loop1, loop2;
    public int auxControl = 0;
    public bool auxB = false, auxA = false;

    public void LoadContent()
    {
        SoundEffect temp1 = Game.Content.Load<SoundEffect>("sounds/Synctest_1");

        loop1 = temp1.CreateInstance();
        loop1.IsLooped = true; 

        loop2 = temp1.CreateInstance();
        loop2.IsLooped = true;
    }

    public override void Update(GameTime gameTime)
    {
        // start first sound
        if (auxA == false)
            loop1.Play(); auxA = true;

        // start counting until 2 seconds
        if (auxA)
            auxControl += gameTime.ElapsedGameTime.Milliseconds;

        // if 2 seconds have passed after first sound start second sound
        if (auxControl >= 2000 && auxB == false)
        {
            loop2.Play();
            auxB = true;
        }

        base.Update(gameTime);
    }

谢谢你

4

2 回答 2

1

对 C# 一无所知,但如果 API 不支持,通常很难以毫秒精度同步这些东西。解决方案是自己混合它们并仅将 API 用于播放,这样您就可以准确控制它们何时播放以及它们如何组合。

在 C# 中可能有一个更简单的解决方案,但您可以使用http://www.PortAudio.com或许多其他接口构建类似的东西。您可能想在 google 上搜索类似 Game audio API 的内容。

于 2012-09-25T03:32:32.337 回答
0

现在我已经决定实现这一点的最佳方法是在更新中使用条件,最好是主更新,因此验证以更快的方式完成。

这仍然带来了一个问题,因为您可能会在声音之间听到一个小的“Tuk”,但几乎没有注意到。

伪代码是这样的

Update(GameTime gameTime)
{

    if (last sound ended)
    {
        play next;
        decide on the next sound; // implement cache here if needed
    }

}
于 2013-02-27T09:58:28.190 回答