我正在使用Windows Phone应用程序,并且遇到以下问题。我有一个循环的.Wav文件用于背景音乐,可以打开或关闭该文件。按下按钮即可播放其他声音效果(例如Sound1)。关闭背景音乐时,声音效果会很好。但是,当打开背景音乐时,按钮的按下和声音效果之间会有微小(但非常明显)的延迟。我可以通过编程来避免这种情况吗?根据MSDN网站http://msdn.microsoft.com/en-us/library/ff431744%28v=vs.92%29.aspx上的建议,我设置了一个dispatcherTimer来调用FrameworkDispatcher。每50毫秒更新一次。但是,这似乎并没有太大帮助...我在下面粘贴了一些代码。我还能做些什么来摆脱这种滞后吗?

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


    }



}

}

最佳答案

预加载SoundEffect,这样就不会从FromStream()获得延迟。但是那不是非常电话友好的。

10-04 21:36