Creating WaveOut device outside the main thread?

Aug 3, 2010 at 11:33 PM
Edited Aug 3, 2010 at 11:59 PM

I'm not sure what the problem is but in a WPF application when I create a WaveOut object in a thread the Read method derived from WaveProvider32 doesn't get called. If I put the code in the main thread it works. As far as I know I'm not using Window callbacks and the thread is still alive. The problem occurs if the 'new WaveOut()' instruction is put in a thread. Maybe I've missed the part where it says WaveOut device must be created in the Main thread ?

My code looks like this:

Thread audioThread = new Thread(new ThreadStart(StartAudioThread));
audioThread.Start();

private void StartAudioThread()
{
    WaveStream ws = new WaveStream();
    IWavePlayer playbackDevice = new WaveOut();
    playbackDevice.Init(ws);
    playbackDevice.Play();

    // WaveStream derived class Read method is called just 3 times before
    // 'while (true)' line is hit in debugger, after Read method is never called again
    while (true)
        Thread.Sleep(200);
}

// Creating the device in the main UI Thread works
IWavePlayer playbackDevice = new WaveOut();

private void StartAudioThread()
{
    WaveStream ws = new WaveStream();
    playbackDevice.Init(ws);
    playbackDevice.Play();

    while (true)
        Thread.Sleep(200);
}
Coordinator
Aug 9, 2010 at 10:11 AM

when you create WaveOut you can call a constructor that specifies the callback mechanism. make sure you are using function callbacks

Mark

Aug 11, 2010 at 11:48 PM

Thank you so much for your reply!

I solved all my problems with Multithread UI freezing playback by changing the constructor call to:

 new WaveOut(WaveCallbackInfo.FunctionCallback());