0

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

我的问题?

如果您按下一个按钮,则声音循环正常,并且当再次按下按钮时它将停止,但是如果您按下一个按钮(所以一个声音正在循环)然后再按另一个(尝试同时播放两种声音)然后第一个声音停止了,我一生都无法弄清楚为什么?

我的代码:

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

非常感谢,

杰克

4

1 回答 1

0

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

于 2012-06-03T01:08:02.070 回答