Reputation: 1052
I have a WAV file that has been recorded using a custom codec. The codec has been installed on my machine, and the WAV file plays fine on my machine using Windows Media player. I am using the NAudio routines to try and play the WAV file via some C# code. The values for the custom format look weird, but I have painstakingly checked them by analysing the WAV file header. Here is the best C# code I have come up with for playing the file:
WaveFormat wfOKI = WaveFormat.CreateCustomFormat(WaveFormatEncoding.DialogicOkiAdpcm, 8000, 1, 3000, 48, 3);
WaveStream wsRaw = new WaveFileReader(txtFileName.Text);
wsRaw = WaveFormatConversionStream.CreatePcmStream(wsRaw); // Line A
wsRaw = new BlockAlignReductionStream(wsRaw); // Line B
WaveStream wsOKI = new RawSourceWaveStream(wsRaw, wfOKI);
WaveOut woCall = new WaveOut();
woCall.Init(wsOKI); // <-- This line gives an error.
woCall.Play();
while (woCall.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(300);
}
The Init() causes the following error: An unhandled exception of type 'NAudio.MmException' occurred in NAudio.dll. Additional information: WaveBadFormat calling waveOutOpen.
Is the code the correct strategy for playing a WAV with a custom codec? I have tried all four combinations of commenting out/in Lines A and B (with no difference to the error message).
I'm using Windows 7 64-bit, Visual Studio 2010 professional (project is set to x86), and version 1.6 of NAudio. I'm very new to NAudio, but did get a few lines going that played a "standard" WAV (i.e. a file that did not use a custom codec).
Upvotes: 3
Views: 3390
Reputation: 49482
If you have got a WAV file then there is no need for the RawSourceWaveStream
. You can play the converted stream directly.
var wsRaw = new WaveFileReader(txtFileName.Text);
wsRaw = WaveFormatConversionStream.CreatePcmStream(wsRaw);
WaveOut woCall = new WaveOut();
woCall.Init(wsRaw);
woCall.Play();
Also, you should not be calling thread.sleep to wait for it to finish if you are using WaveOut. Try WaveOutEvent instead if you are not using this from an application with a GUI.
Upvotes: 3