2012-09-24 206 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 
    } 

}