C# 使用多个按钮播放多种声音
如何使用WindowsPhoneXNA框架让多个按钮一次播放一个不同的声音,并拥有一个通用的停止按钮?播放声音时,声音应循环播放,直到一个按下停止按钮或另一个按钮 我使用C# 使用多个按钮播放多种声音,c#,windows-phone-7,xna,C#,Windows Phone 7,Xna,如何使用WindowsPhoneXNA框架让多个按钮一次播放一个不同的声音,并拥有一个通用的停止按钮?播放声音时,声音应循环播放,直到一个按下停止按钮或另一个按钮 我使用SoundEffect和CreateInstance的方式,它循环播放,但当单击第二个按钮时,第二个声音开始与第一个一起播放。还需要有关创建公共停止按钮的帮助。先谢谢你 我试着为每个按钮做如下操作 private void button2_Click(object sender, RoutedEventArgs e) {
SoundEffect
和CreateInstance
的方式,它循环播放,但当单击第二个按钮时,第二个声音开始与第一个一起播放。还需要有关创建公共停止按钮的帮助。先谢谢你
我试着为每个按钮做如下操作
private void button2_Click(object sender, RoutedEventArgs e)
{
var stream = TitleContainer.OpenStream("Sounds/A3.wav");
var effect = SoundEffect.FromStream(stream);
SoundEffectInstance instance = effect.CreateInstance();
instance.IsLooped = true;
instance.Play();
但是,由于创建的实例不在程序范围内,所以我在创建公共停止按钮时遇到了问题
我是编程的初学者。感谢您的理解。您可以向类和一些帮助器方法添加成员变量:
public class YourClass
{
private SoundEffectInstance currentSoundEffect = null;
private void StopCurrentSoundEffect()
{
this.currentSoundEffect.Stop();
this.currentSoundEffect = null;
}
private void PlaySoundEffect(string fileName)
{
this.StopCurrentSoundEffect();
using (var stream = TitleContainer.OpenStream("Sounds/A3.wav"))
{
var soundEffect = SoundEffect.FromStream(stream);
this.currentSoundEffect = soundEffect.CreateInstance();
this.currentSoundEffect.IsLooped = true;
this.currentSoundEffect.Play();
}
}
}
现在,您的每个事件处理程序都可以使用所需的文件名调用
this.PlaySoundEffect
。您可以展示一个代码示例,让我们看看您在尝试什么吗?