Search code examples
c#visual-studio-2013playsound

Playing sound from application


I have do a small 2d game in C#. I want to add sound. After watching a video in YouTube I have typed the following code but it is not running:

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 Test_Sound
{
    public partial class Form1 : Form
    {
        private SoundPlayer sound;
        public Form1()
        {
            sound = new SoundPlayer("3G.wav");
            InitializeComponent();
        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox.Checked)
            {
                checkBox.Text = "Stop";
                sound.Play();
            }
            else
            {
                    checkBox.Text = "Play";
                    sound.Stop();   
            }
        }
    }
}

Visual Studio is showing the following error:

line :27 Error 1 'System.Windows.Forms.CheckBox' does not contain a definition for 'Play' and no extension method 'Play' accepting a first argument of type 'System.Windows.Forms.CheckBox' could be found (are you missing a using directive or an assembly reference?) c:\users\hp\documents\visual studio 2013\Projects\Test_Sound\Form1.cs

line 32: Error 2 'System.Windows.Forms.CheckBox' does not contain a definition for 'Stop' and no extension method 'Stop' accepting a first argument of type 'System.Windows.Forms.CheckBox' could be found (are you missing a using directive or an assembly reference?) c:\users\hp\documents\visual studio 2013\Projects\Test_Sound\Form1.cs

Can any one help me fix my error?

The Video link is Play Sounds in Windows Forms App (C# .NET)

I have fixed that error. But now i have got exception. Visual Basic showing the following exception message at line number 27. The exception message has given below:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: Sound API only supports playing PCM wave files. I have downloaded a PCM wave file from internet and replaced the existing file by it. But it is not working.


Solution

  • Play()and Stop() are sound's methods that you declared before Form1()initialization.

    Your code must be:

     private void checkBox_CheckedChanged(object sender, EventArgs e)
     {
         if(checkBox.Checked)
         {
             checkBox.Text = "Stop";
             sound.Play();
         }
         else
         {
             checkBox.Text = "Play";
             sound.Stop();
         }
    }
    

    See:
    - http://www.dotnetperls.com/soundplayer
    - https://msdn.microsoft.com/en-us/library/System.Media.SoundPlayer_methods(v=vs.110).aspx

    About your new Exception, PCM (Pulse Code Modulation - https://en.wikipedia.org/wiki/Pulse-code_modulation) is the only one supported by the System.Media.SoundPlayer class. It's the most common WAV format, so most .WAV files just work.

    There are a few tools that can convert audio files. For example, Switch Audio (http://www.nch.com.au/switch/) can convert between formats (even in the free version). You'll need to get your file into a standard, PCM encoded WAV file.