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

Managed to sqeeze a beep on Windows Phone 8.1 with NAudio WasapiOutRT

Feb 20, 2015 at 3:02 PM
Edited Feb 24, 2015 at 10:42 AM

I am very interested into using NAudio for Windows Store universal apps. After discovering that Nuget was refusing to install NAudio on "portable+win81+wpa81" I thought of digging into the code. After a little bit of entrainment with the code, I could make a Windows Phone 8.1 (Universal) app play a beep from the WasapiOutRT lib.

However, somewhere into the depths of WasapiOutRt.PlayThread() I was getting A first chance exception of type 'System.InvalidOperationException' occurred in NAudio.Win8.DLL exception and the playback was stopping. The play code I used was:
WasapiOutRT wasapiOut = new WasapiOutRT(AudioClientShareMode.Shared, 0);
SignalGenerator sig = new SignalGenerator(48000, 1);
wasapiOut.Init(new SampleToWaveProvider(sig));
This probably relates to the changes that I had to do for the WasapiOutRT to be able to even compile :) So, what I did to fire up the WasapiOut under Windows Phone 8.1 Universal was:
  1. Checkout the code (obviously)
  2. Retarget NAudio.Win8 to Windows 8.1 and Windows Phone 8.1 (using Visual Studio 2015 CTP 5, which otherwise refused to handle those projects).
  3. In WasapiOutRt.NativeMethods I had to make code looks like:
[DllImport("api-ms-win-core-synch-l1-2-0.dll", CharSet = CharSet.Unicode, ExactSpelling = false, PreserveSig = true, SetLastError = true)]
internal static extern IntPtr CreateEventExA(IntPtr lpEventAttributes, IntPtr lpName, int dwFlags, EventAccess dwDesiredAccess);

[DllImport("api-ms-win-core-handle-l1-1-0.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);

[DllImport("api-ms-win-core-synch-l1-2-0.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
public static extern int WaitForSingleObjectEx(IntPtr hEvent, int milliseconds, bool bAlertable);
Note the api-ms-win-core-synch-l1-2-0.dll and CreateEventExW - this was as per and

With those changes the code for the simple audio generation could execute and I could hear a beep for a fraction of the second. Then thread would crash and playback would stop with the described exception.

Placing breakpoints in the PlayThread method would make things even more funny - any pause in the execution would make the while-loop not crash and continue playing normally.

What it leads me think is that there is some synch issue with the changes in the event/handle methods (that otherwise would prevent code from compilation).

Anyone having any idea what could be wrong and how it could be fixed?

P.S. Edited the code to take "api-ms-win-core-handle-l1-1-0.dll" and CreateEventExA
Feb 20, 2015 at 7:15 PM
interesting. I'm afraid I haven't tried this myself. What's the stack trace of the InvalidOperationException?
Feb 20, 2015 at 7:22 PM
Not much of a stacktrace there, I'm afraid. Just that PlayThread threw the exception. Will post more if there is something I can dig out on Monday (code is in the office).
Feb 24, 2015 at 10:37 AM
Most interesting!

On my Emulator, I kept getting the "A first chance exception of type 'System.InvalidOperationException' occurred in NAudio.Win8.DLL" exception. No throws, no debug stops - just this in the Debug Output console. No matter UnhandledException and Debug Native and Managed combinations, I was always getting one beep and a line in the output.

My Lumia 830, though, was completely different story - it worked :) Yes, a simple monotonous tone it was, but it was playing and for far more than a second :) It was so eager at playing, that while debugging if I brought the test app in the background, sound would pause and then continue to play from the background.

So, since I will be dealing with Windows Phone and sound in the coming 1-2 months, I will try to push a Windows Phone NAudio port test some more :)

BTW, I discovered an error in my code - CloseHandle is in "api-ms-win-core-handle-l1-1-0.dll".
Feb 27, 2015 at 7:48 AM
great, thanks for keeping us updated