Lama
Lama

Reputation: 158

CSCore - Play audio from MemoryStream

I am new to CsCore. Need some help. I want to build an Audio Recorder, which is able to record mic and Speakers into a file and an external Recorder device. I have successfully finished recording to file. Now I want just the same thing once again, but playing on a certain audio speaker device. I thought of piping into a stream and using this stream as source for a WasapiOut. However, I failed connecting this stream to an IWaveSource.

private Stream _strm;
private WaveWriter _waveWriterFile;
private WaveWriter _wavewriterStream;
private WasapiOut _externalRecorder;

_strm = new MemoryStream();
_wavewriterStream = new WaveWriter(_strm, _SpkCapture.WaveFormat);
IWaveSource ws = new WaveFileReader(_strm);
_externalRecorder.Initialize(ws);
_externalRecorder.Play();

// on Event I do following:
_waveWriterFile.Write(e.Data, e.Offset, e.ByteCount);
_wavewriterStream.Write(e.Data, e.Offset, e.ByteCount);

Everything works fine until I call 'new WaveFileReader(_strm);'. I get an exception (translated from me) "cannot read beyond end of stream". The stream contains data at this point of time. Have 100ms sleep before trying to fetch the stream. I have also tried

IWaveSource ws = new CSCore.MediaFoundation.MediaFoundationDecoder(strm); 

But no luck. I really need help.

Upvotes: 4

Views: 957

Answers (1)

PepitoSh
PepitoSh

Reputation: 1836

Set the stream Position property to zero.

Upvotes: 1

Related Questions