gpt4 book ai didi

c# - 使用 Sound Effect 同时播放音频片段时出现延迟

转载 作者:可可西里 更新时间:2023-11-01 10:28:48 24 4
gpt4 key购买 nike

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

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() 中获得延迟。然而,这对手机来说不是很友好。

关于c# - 使用 Sound Effect 同时播放音频片段时出现延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com