This project has moved and is read-only. For the latest updates, please go here.

Error when play/stop recording

May 15, 2012 at 8:17 PM

Hi Mark,
I have a problem with start/stop playing recording.
I have to play a sample which have to be recorded to disk every time it is played because it could be set with different volume level. I do the following:
Play recording:

recorder = new WaveRecorder(file, recordingFileName);
playbackDevice.Init(recorder);
playbackDevice.Play();

Stop playing:
playbackDevice.Stop();

The problem is that if the user press start/stop several times in a hurry. the following error occurred:
NAudio.MmException: AlreadyAllocated calling waveOutOpen

I know that I must avoid Init() waveOut every time I play but I have to do the recording every time because I need it for later use.
How can I properly release these objects when stop playing?

May 16, 2012 at 9:54 AM

call Dispose when you have finished with any input or output devises

May 16, 2012 at 9:53 PM

I call Dispose only for recorder object. For the playbackDevice I only use 1 instance and just call Play/Stop on every request. I thought it is not a good idea to create new WaveOut instance every time i want to play the recording and then every time call Dispose.

Jun 5, 2012 at 8:14 PM

Because of my karaoke scenario I need to Init my playback device every time with a new recorder stream because I need this recording with a particular level of volume to be mixed later. The exception occurs if I make play/stop iterations several times. If I invoke Init just once then there is no problem with play/stop iterations.

Jun 11, 2012 at 11:01 AM

you can only call Init once on a playback device. The best approach for you might be to create an IWaveProvider that reads from a source that you can swap out. Remember though that you can't change the WaveFormat after calling Init.

Mark

Jun 11, 2012 at 7:38 PM

I made custom WaveRecorder with BeginRecording and EndRecording methods, so I can control when the stream is written to the disk. Thus I call Init just once with one instance of my custom WaveRecorder. I call BeginRecording when start to play the sample and EndRecording when stop playing the sample. I think this solution works for now.

Yavor