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.