I am trying to record audio in UWP using Winmm.dll. After I execute waveInOpen method (https://msdn.microsoft.com/en-us/library/dd743847(v=vs.85).aspx) I always get WAVERR_BADFORMAT no matter the data I use (it also takes a long time abut 5s). My code looks as follows:
Recorder.cs:
Win32.WAVEFORMATEX waveFormatEx = new Win32.WAVEFORMATEX();
waveFormatEx.wFormatTag = (ushort)Win32.WaveFormatFlags.WAVE_FORMAT_PCM;
waveFormatEx.nChannels = 1;
waveFormatEx.nSamplesPerSec = 8000;
waveFormatEx.wBitsPerSample = 16;
waveFormatEx.nBlockAlign = 2;
waveFormatEx.nAvgBytesPerSec = 16000;
Win32.MMRESULT hr = Win32.waveInOpen(ref hWaveIn, deviceId, ref waveFormatEx, delegateWaveInProc, 0, (int)Win32.WaveProcFlags.CALLBACK_FUNCTION);
Win32.cs:
[StructLayout(LayoutKind.Sequential)]
public struct WAVEFORMATEX
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
[DllImport("winmm.dll")]
public static extern MMRESULT waveInOpen(ref IntPtr hWaveIn, int deviceId, ref WAVEFORMATEX wfx, DelegateWaveInProc dwCallBack, int dwInstance, int dwFlags);
The same code with the same parameters works just fine in Windows Forms app (and executes in split second), but in UWP app it does't and i have no idea why. What is the correct format i could use in UWP?
While working with UWP projects, you could keep in mind: it is a limited subset of features (for the sake of security and cross-device compatibility). So, UWP project has nothing to do with Win32 API. And even if your code is compiling/running on the emulator, it will:
So, if we're talking namely about audio, you have to consider WASAPI as it's 100% compatible with UWP https://msdn.microsoft.com/en-us/library/windows/desktop/dd371455(v=vs.85).aspx