I am creating a WP7 application that requires playing various sound effects (at the touch of a button) over looped background music. Background music starts by pressing button 1, and the loops are in order. When I press button 3 (causes a sound effect), when I press it for the first time, the sound effect is superimposed on the background music. However, when I press button 3 again, the background music stops. I canβt understand why this can happen !? I have inserted the relevant parts of the code below. Would thank for any help.
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"); } }
}
source share