Search code examples
c#audiosoundplayer

SoundPlayer and Windows 7 wav file


When I try to play the sound file "Windows Critical Stop.wav" I get the following exception: exec {"Sound API only supports playing PCM wave files."} System.Exception {System.InvalidOperationException}

I understand what PCM is, I just do not know how to either: 1) play the sound file, 2) determine a priori that it is not PCM and will not play, and block the file from being chosen.

Here is the code I am using:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = FileNameTextBox.Text;

try
{
    player.Play();
}
catch (Exception exec)
{
    MessageBox.Show("Sound could not be played: " + exec.ToString());
}

Solution

  • Wave files can be encoded differently (even with the GSM codec), so just examine what codec is used to skip a particular one. Just check the AudioFormat bits.

    https://ccrma.stanford.edu/courses/422/projects/WaveFormat/