Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop playing sound called via SoundEffect.FromStream

I have some Windows Phone 7 code that starts playing a sound using SoundEffect.FromStream. I am using this instead of a normal media object as I need multiple audio clips to be on the one page.

However, based on certain external events I want to stop a particular sound playing. As sounds played via Open Stream are "play and forget" I cannot work out how to reference this playing sound and stop it.

    public void PlaySoundCircus(object sender, RoutedEventArgs e)
    {
        if (audioPlaying == false)
        {
            using (var stream = TitleContainer.OpenStream("circus.wav"))
            {
                var effect = SoundEffect.FromStream(stream);
                FrameworkDispatcher.Update();
                effect.Play();
            }
            audioPlaying = true;
        }
    }
like image 774
Will Gill Avatar asked Dec 22 '22 10:12

Will Gill


1 Answers

You need to create a SoundEffectInstance which will store a reference to your SoundEffect. The SoundEffectInstance 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();
like image 128
keyboardP Avatar answered Dec 24 '22 00:12

keyboardP