Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# - Play WAV Audio
#1
If you want to simply play a WAV audio file, or play WAV audio files in certain patterns, you can easily use the "MEDIA" library supplied by microsoft without any need to reference 3rd Party DLLs, the code is very easy.

The following code demonstrates how to play a WAV file embed in the app assembly ("Resources"), with "try catch" to handle errors dynamically.

Code:
[U]using System.Media;[/U]

namespace PlayWavFiles
{
    public partial class DMain : Form
    {
        public DMain()
        {
            InitializeComponent();
        }


private void DHitPlay_Click(object sender, EventArgs e)
{
    try
    {
        SoundPlayer wavplayer = new SoundPlayer(PlayWavFiles.Properties.Resources.SampleWAVfile);
        if (DHitPlay.Text == "Play!")
        {
            wavplayer.PlayLooping();
            DHitPlay.Text = "Stop!";
        }
        else
        {
            wavplayer.Stop();
            DHitPlay.Text = "Play";
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

That's it.
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Users browsing this thread: 1 Guest(s)