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

Deadly 提交于 2020-01-11 09:32:24

问题


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

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

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

What is odd is that the code works in the legacy Delphi app - all of the 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, indeed, a difference. For the file that does play, its audio format is PCM; the one that won't 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 download from an url and then played locally) OR perhaps a different way of playing these files than PlaySync() ...


回答1:


Look at audiolab library from mitov. It works great




回答2:


So, do you want to PLAY the file or CONVERT it ? What is the primary goal ? Do you play it as a prove you can convert it, or do you convert it because you don't know how to play not-converted file ? http://www.catb.org/esr/faqs/smart-questions.html#goal

Your question's title claims "convert" but the body claims "Play"

This answer is about playing files.


You also may try to use FFDShow codecs directly without DirectX intermediate. http://en.wikipedia.org/wiki/Libavcodec and http://libav.org/ and http://ffmpeg.org/ (they recently had a schism)

Googling for "FFDShow dotnet", "libav dotnet", "ffmpeg dotnet" shows a bunch of libraries to use it, such as

  • https://github.com/ermau/libav.net
  • Controlling ffdshow from .Net
  • Solid FFmpeg wrapper for C#/.NET

There is also BASS library. It is targeted as sound playback during gaming, so it probably has less range of formats and not much for re-coding. Still many music players are built on top of it. Some says it is the most simple API to use. So it worth considering. http://www.un4seen.com/


http://MediaInfo.sf.net is a library (native win32/win64 DLL) allowing to check most multimedia formats content. I don't know if using tis C or C++ APis is easy from C# side.




回答3:


The way to do it is to use newkie's 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 of the lowercase x's to uppercase x's, though, to get it to work.



来源:https://stackoverflow.com/questions/12393707/how-to-play-non-pcm-file-or-convert-it-to-pcm-on-the-fly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!