1
votes

Using the code:

using (var reader = new WaveFileReader(audioFileLocation))
{
    // Do something....
}

If given a wav file that throws the exception:

Not a WAVE file - no RIFF header
Exception Details: System.FormatException: Not a WAVE file - no RIFF header

It locks the file audioFileLocation which prevents it from being deleted.

Is there any way to check for the existence of a valid RIFF header before using the reader?

2
Most likely there is a bug in WaveFileReader in that it doesn't close the file. Where did you get that class? - Lasse V. Karlsen
How long is the file locked? Until the process terminates or a short while? - Lasse V. Karlsen
It's locked until I restart IIS. It's the NAudio.Wave.WaveFileReader class - Tom Gullen
The WaveFileReader class has a bug here. According to the source code on codeplex, here: naudio.codeplex.com/SourceControl/latest#NAudio/Wave/…, it checks if the file is a valid RIFF file before setting the ownInput parameter, and thus doesn't close the stream if given an invalid file. - Lasse V. Karlsen
@Lasse thanks will submit a bug report - Tom Gullen

2 Answers

2
votes

Try using a stream:

using(var inputStream = new FileStream(audioFileLocation, FileMode.Open, 
                           FileAccess.Write, FileShare.ReadWrite))
{
    using (var reader = new WaveFileReader(inputStream))
    {
        // Do something....
    }
}

If THIS is the current code of the WaveFileReader class it uses File.OpenRead(waveFile) in the "string overload" of the consturctor and the Stream returned seems not to be closed/disposed. Maybe already the follownin works:

using(var inputStream = File.OpenRead(audioFileLocation))
{
    using (var reader = new WaveFileReader(inputStream))
    {
        // Do something....
    }
}

as this should dispose the stream.

0
votes

I had the same code, but just before that code I was getting my FileWriter disposed, it was writing to audioFileLocation

fileWriter.Dispose; // writes to audioFileLocation
// ...
using (var reader = new WaveFileReader(audioFileLocation))
{
    // Do something....
}

Some computers running the code were too slow to reset to initial position, resulting of searching for wave header at the end of the file. The fix was to reset the position:

using (var reader = new WaveFileReader(audioFileLocation))
{
    reader.Position = 0;
    // Do something....
}