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

How to use NAudio in WPF?

Jan 4, 2010 at 6:16 PM

 

I'm trying to use NAudio in WPF, but I have one big problem. I use AudioPlayback (WaveOut playback device) class from NAudioWpfDemo sample project and everything works well with first sound file, but when I try to load and play next sound I've received a NullReferenceException. The same problem is with original NAudioWpfDemo project. I suppose that the problem is with handling messages from WaveOut, but I don't know how to handle them in WPF. I tried this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
IntPtr hwnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
        if (msg >= 0x3BB && msg <= 0x3C0)
{
                int x = 0;
         x++;
}
return IntPtr.Zero;
}
And than I create playbackDevice in AudioPlayback class like this:
public AudioPlayback(IntPtr aHwnd)
{
hwnd = aHwnd;
}
private void CreateDevice()
{
WaveCallbackInfo callbackInfo = WaveCallbackInfo.ExistingWindow(hwnd);          
playbackDevice = new WaveOut(callbackInfo); 
}
But I have never received any messages in WndProc in range >= 0x3BB <= 0x3C0 Which should be messages from WaveOut. I tried WaveCallbackInfo callbackInfo = WaveCallbackInfo.NewWindow() also, but this didn't solve the NullReferenceException problem either.

 

I'm trying to use NAudio in WPF, but I have one big problem. I use AudioPlayback (WaveOut playback device) class from NAudioWpfDemo sample project and everything works well with first sound file, but when I try to load and play next sound I've received a NullReferenceException. The same problem is with original NAudioWpfDemo project. I suppose that the problem is with handling messages from WaveOut (because if I make small pause between AudioPlayback.Stop() and AudioPlayback.OpenFile() it works), but I don't know how to handle them in WPF. I tried this:

 

private void Window_Loaded(object sender, RoutedEventArgs e)
{
	IntPtr hwnd = new WindowInteropHelper(this).Handle;
	HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
}

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
        if (msg >= 0x3BB && msg <= 0x3C0)
	{
                int x = 0;

        	x++;
	}

	return IntPtr.Zero;
}

 

 

And than I create playbackDevice in AudioPlayback class like this:

 

public AudioPlayback(IntPtr aHwnd)
{
	hwnd = aHwnd;
}

private void CreateDevice()
{
	WaveCallbackInfo callbackInfo = WaveCallbackInfo.ExistingWindow(hwnd);          
	playbackDevice = new WaveOut(callbackInfo); 
}

But I have never received any messages in WndProc in range >= 0x3BB <= 0x3C0 which should be messages from WaveOut. I tried WaveCallbackInfo callbackInfo = WaveCallbackInfo.NewWindow() also, but this didn't solve the NullReferenceException problem either.

Is there a solution? Thank you.

 

 

Jan 24, 2010 at 10:30 PM
Edited Jan 24, 2010 at 10:31 PM

Maybe it could be related to the the callback via Form in wpf
Try creating a Form object in your main-window.cs and use that

Form f = new Form();
IntPtr hwnd = new WindowInteropHelper(f).Handle;

Edit:
Note I havent looked at WindowInteropHelper for a long time.