Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导航到其他窗体时如何再次停止声音播放_C#_Audio - Fatal编程技术网

C# 导航到其他窗体时如何再次停止声音播放

C# 导航到其他窗体时如何再次停止声音播放,c#,audio,C#,Audio,我目前正在用c#编写一个应用程序,该应用程序启动后将播放背景音乐。当我按下静音按钮时,按钮点击的声音和音乐将停止,或在取消静音时再次播放 namespace WindowsFormsApp1 { public partial class mainMenu : Form { public bool sound = true; //variable of whether the sound is on or not System.Media.SoundPlayer bgm = new

我目前正在用c#编写一个应用程序,该应用程序启动后将播放背景音乐。当我按下静音按钮时,按钮点击的声音和音乐将停止,或在取消静音时再次播放

namespace WindowsFormsApp1
{
public partial class mainMenu : Form
{
    public bool sound = true; //variable of whether the sound is on or not
    System.Media.SoundPlayer bgm = new System.Media.SoundPlayer(@"C:\Users\User\Desktop\HCI\New folder\bgm.wav");

    public bool soundval //for other form to access sound variable value
    {
        get
        {
            return sound;
        }

        set
        {
            sound = value;
        }
    }

    public mainMenu()
    {
        InitializeComponent();
        play();
    }

    public void play() //function to play music
    {
        bgm.PlayLooping();
    }

    public void stop//function to stop music
    {
        bgm.Stop();
    }
    private void soundButton_Click(object sender, EventArgs e) //button that mute or unmute the sound (by changing sound value)
    {
        if (sound)
        {
            bgm.Stop();
            sound = false;
        }
        else
        {
            var p2 = new System.Windows.Media.MediaPlayer();
            p2.Open(new System.Uri(@"C:\Users\User\Desktop\HCI\New folder\button.wav"));
            p2.Play();
            play();
            sound = true;
        }
    }
    private void easy_Click(object sender, EventArgs e) //proceed to other form
    {
        if (sound)
        {
            var p2 = new System.Windows.Media.MediaPlayer();
            p2.Open(new System.Uri(@"C:\Users\User\Desktop\HCI\New folder\button.wav"));
            p2.Play();
        }

        Easy game2 = new Easy();
        game2.Tag = this;
        game2.Show(this);
        Hide();
    }
    }
}
在此之前,该应用程序运行良好。但是,如果我将声音静音并转到其他形式,音乐将再次播放,如果我按下该特定形式上的任何按钮,按钮声音也将播放

我的其他表单的部分代码(轻松单击):

不仅是音乐,还有按键的声音也在播放。当继续到第二种形式时,看起来声音值设置为true,即使第一种形式为false。 我在这个问题上被困了很长一段时间,到处都在改变,但没有任何效果。
如果有人能帮上忙,我真的很感激。谢谢

没有,现在已经读完了。但在这里,我可以停止声音,但一旦应用程序导航到另一个窗体,它会再次(自动)播放。我已经删除了重复的You write
new Main Menu
,这意味着它将从启动时加载默认值。但是如果你希望改变的值要比不使用<代码>新的主菜单< /代码>,因为它将加载新的类,并且在主菜单中设置<代码> SoSalval= true.<代码> >我明白了,这解释了为什么,我将尝试解决这个问题。谢谢@Abhiverma
mainMenu mmenu = new mainMenu();
private void thirdchoice_Click(object sender, EventArgs e)
    {
        if (mmenu.soundval) //to get the value from first form, button sound should play only when value is true
        {
            var p2 = new System.Windows.Media.MediaPlayer();
            p2.Open(new System.Uri(@"C:\Users\User\Desktop\HCI\New folder\button.wav"));
            p2.Play();
        }
        if (ansNum == choice3num) goodnews();
        else badnews();
    }