Thursday, November 10, 2011

Play sound and Stop sound (Windows Phone )


Windows-Phone-7: Stop playing sound called via SoundEffect.FromStream


You need to create a SoundEffectInstance which will store a reference to your SoundEffect. TheSoundEffectInstance has a Stop method which can be called.
SoundEffectInstance seiCircus;

using
(var stream = TitleContainer.OpenStream("circus.wav"))
{
   
var effect = SoundEffect.FromStream(stream);
   
//create the instance
    seiCircus
= effect.CreateInstance();

   
FrameworkDispatcher.Update();
   
//play sound via the instance
    seiCircus
.Play();
}
//some event called to stop sound
seiCircus
.Stop();