(wL) Forums

Full Version: C# - Play WAV Audio
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.