how to play a sounds in c# forms?

59 views Asked by At

I work in a windows form application. i wanna make a setting page for my program. in this form i have music for the total of my program and i have sound for my button, it sounds when you click on a button. i can off them and on them. my problem, is sounds of buttons. when i off the sound in the setting and it just off for the setting form, and when i go to another form and setting page is still open the sound of button is not off. and it sound when i click.

page setting:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using WMPLib;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
       WindowsMediaPlayer playerr = new WindowsMediaPlayer();
       public WindowsMediaPlayer button = new WindowsMediaPlayer();
        public bool s ;
        public bool p ;


        public Form1()
        {
            InitializeComponent();
            this.StartPosition= FormStartPosition.Manual;
            this.Location = new Point(300, 150);
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            playerr.URL = "aurora_runaway.mp3";
            playerr.controls.play();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (pictureBox3.Visible == false && pictureBox4.Visible == true)
            {
                button.URL = "notifications-sound-127856.mp3";
                button.controls.play();
            }
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {
            playerr.controls.play();
            pictureBox1.Visible= false;
            pictureBox2.Visible= true;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            playerr.controls.stop();
            pictureBox2.Visible = false;
            pictureBox1.Visible =true;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
           
            pictureBox3.Visible = false;
            pictureBox4.Visible = true;
            s = true;
            p = false;

        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            pictureBox4.Visible = false;
            pictureBox3.Visible = true;
            s = false;
            p = true; 

        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            new Form3().ShowDialog();
        }
    }
}

form3:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form3 : Form
    {
        Form1 ff = new Form1();

        public Form3()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (ff.p == true && ff.s == false)
            {
                ff.button.URL = "notifications-sound-127856.mp3";
                ff.button.controls.play();
            }
          
        }
    }
}

pleas help me

0

There are 0 answers