简体   繁体   English

我怎么知道我是否可以使用System.Media.SoundPlayer播放Wave文件

[英]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. 某些wav文件由于某些原因无法播放。 For example when trying to play this wave file I get an error: 例如,当尝试播放此wave文件时,出现错误:

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 我正在寻找一种可以告诉我是否可以播放wave文件的方法,例如这种方法

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 . 所以我怎么知道波形文件是否具有这种PCM格式 It will be nice if a method SoundPlayer.CanPlayFile() existed. 如果存在方法SoundPlayer.CanPlayFile()会很好。

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... 我只是想知道是否可以在不使用try catch块的情况下播放它们...

WAV files can have multiple compression methods, including MP3. WAV文件可以具有多种压缩方法,包括MP3。 PCM (Pulse Code Modulation) is the only one supported by the System.Media.SoundPlayer class. PCM(脉冲代码调制)是System.Media.SoundPlayer类唯一支持的一种。 Fortunately, it's the most common WAV format, so most .WAV files just work. 幸运的是,它是最常见的WAV格式,因此大多数.WAV文件都可以使用。

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/d30a3002-a553-4bb2-b8da-058255395a5e/ 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. 您要么将这些WAV转换为PCM,要么必须使用另一个Media Player。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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