停止播放通过SoundEffect.FromStream调用的声音

Wil*_*ill 4 c# windows windows-phone-7

我有一些Windows Phone 7代码,使用SoundEffect.FromStream开始播放声音.我正在使用它而不是普通的媒体对象,因为我需要在一个页面上有多个音频剪辑.

但是,根据某些外部事件,我想停止特定的声音播放.由于通过开放流播放的声音是"玩和忘记"我无法弄清楚如何引用这种播放声音并停止它.

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

key*_*rdP 11

您需要创建一个SoundEffectInstance,它将存储对您的引用SoundEffect.所述SoundEffectInstance具有Stop可被调用的方法.

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();
Run Code Online (Sandbox Code Playgroud)