简体   繁体   中英

how can I know if I can play a wave file with System.Media.SoundPlayer

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM