How to check if a sound file is a .wav file in C# ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rocco1010
    New Member
    • Mar 2010
    • 2

    #1

    How to check if a sound file is a .wav file in C# ?

    Let's say somebody may change the extension of a .doc or .mp3 file to .wav, in my application, I want to check to make sure if a file is actually a .wav file.

    How do I do this in C# ?
  • EARNEST
    New Member
    • Feb 2010
    • 128

    #2
    Maybe System.IO - > GetExtension?
    Or check certain area of the file (header maybe?) where that info can exist.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Read the file header byte by byte

      Comment

      • EARNEST
        New Member
        • Feb 2010
        • 128

        #4
        :D very close.
        anyways,this might help the OP

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          If you don't care too much about program speeds, you can always just try and play it anyway and use a try/catch block to handle the exception. I don't know what you're using to play the sound, but I did some tests with a System.Media.So undPlayer object and it seems a System.InvalidO peration exception is thrown when it tries to play a file that's not a valid wav file.

          Code:
              public partial class Form1 : Form
              {
                  System.Media.SoundPlayer m_player = new System.Media.SoundPlayer();
          
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  private void button1_Click(object sender, EventArgs e)
                  {
                      try
                      {
                          m_player.SoundLocation = textBox1.Text;
                          m_player.Play();
                      }
                      catch (System.IO.FileNotFoundException ex)
                      {
                          MessageBox.Show("Could not file file: " + textBox1.Text + "\n" + ex.Message);
                      }
                      catch (System.InvalidOperationException ex)
                      {
                          MessageBox.Show("File was not a valid .WAV file: " + textBox1.Text + "\n" + ex.Message);
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show("Unknown exception: " + ex.Message);
                      }
                  }
              }
          If you're loading and playing a lot of files in quick succession though, the overhead of a try/catch might make it so this method isn't right for you, in which case you'll probably need to go with T's option.

          Comment

          • rocco1010
            New Member
            • Mar 2010
            • 2

            #6
            GarryTexmo: if we try to play the sound anyway, it still cannot detect if it's a .wav or, .mp3, .midi...etc.

            tlhintoq: "Read the file header byte by byte" => not all files contain a header ?

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Then you just have to try to analyse the file and see if it fits what you *think* it is.

              If think it is supposed to be an MP3, then see if the frames fit the specification.

              A .wav has a header


              midi has a header

              Comment

              Working...