【问题标题】:Stop playing sound called via SoundEffect.FromStream停止播放通过 SoundEffect.FromStream 调用的声音
【发布时间】:2011-08-10 20:41:39
【问题描述】:

我有一些 Windows Phone 7 代码开始使用 SoundEffect.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;
        }
    }

【问题讨论】:

    标签: c# windows windows-phone-7


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多