停止播放通过soundeeffect . fromstream调用的声音

本文关键字:fromstream 调用 声音 soundeeffect 播放 | 更新日期: 2023-09-27 18:05:03

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

然而,基于某些外部事件,我想要停止特定的声音播放。由于通过Open Stream播放的声音是"播放并忘记"的,我不知道如何引用此播放声音并停止它。

    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;
        }
    }

停止播放通过soundeeffect . fromstream调用的声音

您需要创建一个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();