How to play a non-PCM file or convert it to PCM on the fly?

The following code works with some wav files, but with the others I get, "InvalidOperationException was unhandled. Message = Sound API only supports playing files in PCM format."

var webClient = new WebClient(); webClient.DownloadFile(url, fileName); var fileSound = new SoundPlayer(fileName); fileSound.PlaySync(); 

Is there a way to programmatically check if the wav file is "bad" (and not a PCM wave file) and then convert it as needed?

What is strange is that the code works in an outdated Delphi application - all wav files play just fine. Here's the Delphi code:

 filename := GetEnvironmentVariable('TEMP')+'\archieAndDingbat.wav'; URLDownloadToFile(nil, PChar(url), PChar(filename), 0, nil); PlaySound(filename); 

I looked at the properties of the two files in Explorer and I see that there is a difference. For a file that is being played, its audio format is PCM; what will not play is CCITT u-Law.

So ... I either need a way to convert from CCITT u-Law to PCM on the fly after downloading these files (they are downloaded from a URL and then played locally), or maybe another way to play these files is PlaySync () ...

+4
source share
3 answers

The way to do this is to use the newkie code at: http://www.codeproject.com/Articles/175030/PlaySound-A-Better-Way-to-Play-Wav-Files-in-C?msg=4366037#xx4366037xx

In my case, at least I had to change all lowercase letters x to uppercase x to make it work.

0
source

Take a look at the Mith Audio Lab Library. It works great

+1
source

So, do you want to play the file or convert it? What is the main goal? Do you play it as proof that you can convert it, or do you convert it because you don’t know how to play an unprocessed file? http://www.catb.org/esr/faqs/smart-questions.html#goal

Your question title claims to be "convert", but the body claims to be "Play"

This answer is about playing files.


You can also try using FFDShow codecs directly without an intermediate DirectX layer. http://en.wikipedia.org/wiki/Libavcodec and http://libav.org/ and http://ffmpeg.org/ (they recently had a split)

Googling for "FFDShow dotnet", "libav dotnet", "ffmpeg dotnet" shows a bunch of libraries for using it, for example


There is also a BASS library. It is designed to play sound during games, so it probably has a smaller range of formats and is not very suitable for re-encoding. There are still a lot of music players on it. Some say this is the easiest API to use. Therefore, it is worth considering. http://www.un4seen.com/


http://MediaInfo.sf.net is a library (native win32 / win64 DLL) that allows you to check the contents of most multimedia formats. I don't know if tis C or C ++ APis is easy to use from C # side.

+1
source

Source: https://habr.com/ru/post/1433864/


All Articles