在Windows Phone 7中循环播放声音

时间:2012-06-03 00:59:24

标签: c# windows-phone-7 soundeffect

按下按钮时,我试图让声音循环,然后再次按下相同按钮时停止声音。我有多个同时播放的声音(都是循环播放),所有声音应该能够以任何顺序开始和停止。

我的问题?

如果您按下一个按钮,则声音会很好地循环,当再次按下该按钮时它会停止,但是如果您按一个按钮(这样一个声音正在循环播放)然后按另一个按钮(尝试播放两个声音)同时)然后第一个声音停止了,我不能为我的生活弄清楚为什么?

我的代码:

    private void button3_Click(object sender, RoutedEventArgs e)
    {


        SoundEffect sound3;
        int x = 0;

        StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri("tracks/drum (human).wav", UriKind.Relative));

        sound3 = SoundEffect.FromStream(SoundFileInfo.Stream);

        SoundEffectInstance sound3instance = sound3.CreateInstance();


        if (button3.Content.Equals("Sound 3"))
        {
            sound3instance.IsLooped = true;
            sound3instance.Play();
            button3.Content = "Playing";
        }
        else
        {
            sound3instance.Pause();
            button3.Content = "Sound 3";
        }

    }

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        SoundEffect sound4;

        StreamResourceInfo SoundFileInfo2 = App.GetResourceStream(new Uri("Cow Moo 1.wav", UriKind.Relative));

        sound4 = SoundEffect.FromStream(SoundFileInfo2.Stream);

        SoundEffectInstance sound4instance = sound4.CreateInstance();


        if (button4.Content.Equals("Sound 4"))
        {
            sound4instance.IsLooped = true;
            sound4instance.Play();
            button4.Content = "PLaying";
        }
        else
        {
            sound4instance.Pause();
            button4.Content = "Sound 4";
        }
    }

非常感谢,

杰克

1 个答案:

答案 0 :(得分:0)

是sound3instance和sound4instance存储在方法之外吗?