1

To play I wave file I do:

SoundPlayer p = new SoundPlayer(@"myWaveFile.wav");
p.Play();

Some wav files cannot play for some reason. For example when trying to play this wave file I get an error:

SoundPlayer p = new SoundPlayer(@"c:\Windows\Media\Afternoon\Windows Balloon.wav");
p.Play();
// the exception that I get is:   Sound API only supports playing PCM wave files.

I am looking for a method that could tell me if I could play the wave file such as this method

public void CanPlayWaveFile(string file)
{
        SoundPlayer p = new SoundPlayer(file);
        try
        {
            p.Play();
            return true;
        }
        catch
        {
            return false;
        }
}

the problem with that method is that I might not want to play the audio I just want to know if I can play that file. Also I don't want to have to wait for the wave file to finish playing in order to see if I can play that file later or not. So how can I know if a wave file has this PCM format. It will be nice if a method SoundPlayer.CanPlayFile() existed.

Edit

I see people help me see how I can play other formats or convert this format. I don't want to convert it to other formats. I just want to know if I can play them or not without having to have the try catch block...

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • Read [this](http://stackoverflow.com/questions/12393707/how-to-play-non-pcm-file-or-convert-it-to-pcm-on-the-fly) – aef Jun 04 '13 at 16:10

1 Answers1

1

WAV files can have multiple compression methods, including MP3. PCM (Pulse Code Modulation) is the only one supported by the System.Media.SoundPlayer class. Fortunately, it's the most common WAV format, so most .WAV files just work.

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/d30a3002-a553-4bb2-b8da-058255395a5e/

Either you convert these WAV to PCM or you have to use another Media Player.

Mohammad abumazen
  • 1,286
  • 1
  • 11
  • 24