停止播放通过 SoundEffect.FromStream 调用的声音 [英] Stop playing sound called via SoundEffect.FromStream

查看:10
本文介绍了停止播放通过 SoundEffect.FromStream 调用的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是,基于某些外部事件,我想停止播放特定的声音.由于通过 Open Stream 播放的声音是播放后忘记",我无法弄清楚如何引用此播放声音并停止播放.

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

推荐答案

您需要创建一个 SoundEffectInstance 将存储对您的 SoundEffect 的引用.SoundEffectInstance 有一个可以调用的 Stop 方法.

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();

这篇关于停止播放通过 SoundEffect.FromStream 调用的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆