【发布时间】:2011-08-10 11:09:06
【问题描述】:
我正在创建一个 WP7 应用程序,该应用程序需要在循环背景音乐上播放各种音效(按下按钮)。背景音乐通过按下按钮 1 启动并循环播放。当我按下按钮 3(触发音效)时,音效会在第一次按下时很好地覆盖在背景音乐上。但是,当我再次按下按钮 3 时,背景音乐停止。我无法弄清楚为什么会发生这种情况!?我在下面粘贴了代码的相关部分。将不胜感激。
public partial class MainPage : PhoneApplicationPage
{
SoundEffect soundEffect;
Stream soundfile;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
{
SoundEffectInstance instance = soundEffect.CreateInstance();
instance.IsLooped = true;
FrameworkDispatcher.Update();
instance.Play();
}
}
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
effect.Play();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
soundfile = TitleContainer.OpenStream("BackgroundMusic.wav");
soundEffect = SoundEffect.FromStream(soundfile);
LoopClip(soundEffect);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
}
【问题讨论】:
-
你尝试过线程或后台工作者吗?
-
这与我在 Java 中不断发生的事情完全相反......
标签: c# windows visual-studio-2010 windows-phone-7