This project has moved. For the latest updates, please go here.

Play WAV file that is still being written to

Oct 6, 2015 at 2:17 PM
Edited Oct 6, 2015 at 2:18 PM

I searched for an answer, but I did not find any.

The problem:
I have a situation where I must play a WAV-file that may be still being written to. Part of the problem I solved (by opening the file using a Stream instead of a file, because the WAV file is obviously locked by another process).
However, I now can access the file, but I only can play the portion that was available when opening the file. I want the length to be dynamic, it has to shift in time.

So here's my question:
How do I update the 'TotalTime' of the Wave object to reflect the current length? I can set some timer in order to do the refresh, but how do I trigger a refresh in the Wave object?

If anyone could give me a direction to a solution. It would help me a lot.
Oct 10, 2015 at 11:17 AM
Edited Oct 10, 2015 at 11:18 AM
Hi, do you want to record from web stream and play the stream at the same time? Or do you only want to write "what you listen" when playing back a file?

Your approach isn´t clear to me. So I will show you both:

For case 1#: Look at the NAudio radio streaming demo
For case 2#: Use SavingWaveProvider
Oct 12, 2015 at 7:42 AM
Thank you for your reply but it is neither of those options.
Let me be a bit more specific.

Some process is writing a WAV file somewhere on a network drive. I do not have any control over that process, so I must take this for a fact.
But I need to be able to play this file already, while it is being written to.
I have managed to do this, but I can only play the portion that was available when I opened the file. But since the recording is still in progress, the actual length of the file is larger. When I then re-open the file again, I can play a larger portion, but again, up 'till the moment I re-opened the file. However, that is of course no option, for it would create a hickup in the sound being played.

So my question again: Can I dynamically change the 'end point' of the WaveFileReader object? Or is there some other mechanism I should use?

The code I'm using at this moment is this:
Stream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
          m_waveReader = new WaveFileReader(stream);
Or should I use some other stream somehow?
Oct 12, 2015 at 1:51 PM
Okay, looks like a general filestream problem. You could perhaps use this approaches:

Another option could be to handle that the file got enlarged (event or sth) and then just re-create the filestream. Ofc, that would restart playback and some delay; you would have to jump to the last known position then.

Anyway this is not a NAudio issue and therefore you would have to find a solution yourself. Just copy the WaveFileReader class and modify it.
Oct 12, 2015 at 2:49 PM
You're right (of course).

Thanks for pointing me the direction!