2012-09-24 47 views
0

我在XNA创造一个游戏,将有很多音乐的工作遍历对方,但我似乎不能够同步这些声音。C#音乐/声音同步

我总是想念几毫秒你能帮我吗?

这是我第一次尝试同步声音。要知道我需要几十声音的工作...

也许这个问题的同步与缓存的声音有关系吗?
有没有一个外部库使它更容易?

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); 
    } 

谢谢

+0

如果所有的循环是相同的长度(或其倍数),你可以使用一个DispatcherTimer优先级高玩上设定的时间间隔每个新以获得更高精度和更干净的代码在YMMV之前从未尝试过。 –

回答

1

不知道C#的东西,但通常很难这些事情与同步精确到毫秒,如果API不支持它。解决方案是自己混合并使用API​​来进行播放,这样您就可以控制播放时的确切时间以及它们的组合方式。

有可能是在C#中的简单的解决方案,但你可以建立这样的事情与http://www.PortAudio.com或许多其他接口。您可能需要在Google上搜索诸如游戏音频API之类的内容。

0

现在我已经决定,最好的方式实现这一使用条件更新内,最好是主更新,所以在完成验证更快的方式,就可以了。

这仍然会带来一个问题,因为您可能会听到声音之间的小“嘟嘟声”,但很难注意到它。

伪代码将是这样的

Update(GameTime gameTime) 
{ 

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

}