我正在使用Windows Phone應用程序,並遇到以下問題。我有一個可以打開或關閉背景音樂的循環.Wav文件。附加聲音效果(例如Sound1)可以在按下按鈕時播放。當背景音樂關閉時,音效效果會很好。但是,當背景音樂開啓時,按鈕按下和音效之間會有輕微的(但非常明顯的)延遲。有什麼我可以編程方式來避免這種情況?根據MSDN網站http://msdn.microsoft.com/en-us/library/ff431744%28v=vs.92%29.aspx的建議,我設置了一個dispatcherTimer來每50ms調用一次FrameworkDispatcher.Update。但是,這似乎沒有太多幫助...我已經粘貼了一些我的代碼。還有什麼我可以做的,以擺脫這種滯後?使用SoundEffect播放同步音頻片段時出現延遲
public partial class Modules : PhoneApplicationPage
{
static SoundEffectInstance loopedSound = null;
static SoundEffectInstance sound1=null;
double pitchValue = 4.0;
static double pitchAdjust = 0.0;
public Modules()
{
InitializeComponent();
// Timer to simulate the XNA game loop (SoundEffect classes are from the XNA Framework)
DispatcherTimer XnaDispatchTimer = new DispatcherTimer();
XnaDispatchTimer.Interval = TimeSpan.FromMilliseconds(50);
XnaDispatchTimer.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
XnaDispatchTimer.Start();
}
private void playButton_Click(object sender, RoutedEventArgs e)
{
if (loopedSound!=null)
loopedSound.Dispose();
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(@"BackgroundSound.wav", UriKind.Relative)).Stream);
LoopClip(sound);
}
private void stopButton_Click(object sender, RoutedEventArgs e)
{
loopedSound.Stop();
}
static protected void LoopClip(SoundEffect soundEffect)
{
loopedSound = soundEffect.CreateInstance();
loopedSound.IsLooped = true;
loopedSound.Pitch = (float) pitchAdjust;
loopedSound.Play();
}
static protected void sound1Clip(SoundEffect soundEffect)
{
sound1 = soundEffect.CreateInstance();
sound1.IsLooped = false;
sound1.Play();
}
private void Sound1Button_Click(object sender, RoutedEventArgs e)
{
if (sound1 == null)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(@"sound1.wav", UriKind.Relative)).Stream);
sound1Clip(sound);
}
if (sound1 != null)
{
sound1.Play();
}
}
}
}
我一直認爲,有一個基本問題,使同步視頻和音頻近似練習..我會有興趣,看看有人知道.. – 2011-04-23 20:59:39