0

I am new to C#, and am making a metronome. I am using SoundPlayer to play some wav files I have in a folder when the user clicks a play button. However, for the pause button the new SoundPlayer I have created is not available to stop/pause. How can/can I make this global? Here is my code:

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;

namespace Metronome
{
    public partial class Form1 : Form
    {
        int mov;
        int movX;
        int movY;
        decimal speed;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mov = 1;
            movX = e.X;
            movY = e.Y;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if(mov==1)
            {
                this.SetDesktopLocation(MousePosition.X - movX, MousePosition.Y - movY);
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            mov = 0;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            speed = BPMTime.Value;
            speed = Decimal.ToInt16(speed);
            string num = speed.ToString();
            SoundPlayer sp = new SoundPlayer(@"C:\Users\user\Desktop\Metronome\Metronome\Sounds\" + num + " BPM.wav");
            sp.PlayLooping();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }
    }
}

pictureBox2 is my pause button. However. when I try to control the SoundPlayer from it, it can't find an instance of it, because (I'm assuming) it was defined inside my Play Button/Picture Box 1 click event. Is there any way I can change this? Any help would be greatly appreciated.

  • 2
    Create that variable in the form's declarations section, where you created your mov, movX, movY, and speed variables. – Honeyboy Wilson Mar 27 '20 at 15:53
  • I tried this initially, but couldn't figure out how, if I do that, how do I declare the file path. I've tried sound location, and it doesn't work, – Amateur_Coder Mar 27 '20 at 15:55
  • In the declarations section you create a reference: SoundPlayer sp. Then in the picturebox click event you set it to a new instance: sp = new SoundPlayer(@"C:\Users\user\Desktop\Metronome\Metronome\Sounds\" + num + " BPM.wav"); – Honeyboy Wilson Mar 27 '20 at 16:00
  • From your description, your program should have exactly one instance of `SoundPlayer`, and you want access to it globally. That's the "Singleton" pattern. See marked duplicate. – Peter Duniho Mar 27 '20 at 17:10

0 Answers0