我正在尝试使用Winmm.dll在UWP中录制音频。在我执行waveInOpen方法(https://msdn.microsoft.com/en-us/library/dd743847(v=vs.85).aspx)后,无论我使用何种数据,我总是得到WAVERR_BADFORMAT(它也需要很长时间才能接近5s)。我的代码如下:
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);
具有相同参数的相同代码在Windows窗体应用程序中运行良好(并在瞬间执行),但在UWP应用程序中它确实没有,我不明白为什么。我可以在UWP中使用的正确格式是什么?
答案 0 :(得分:1)
在使用UWP项目时,您可以牢记:它是功能的有限子集(出于安全性和跨设备兼容性的考虑)。因此,UWP项目与Win32 API无关。即使你的代码在模拟器上编译/运行,它也会:
所以,如果我们谈论音频,你必须考虑WASAPI,因为它与UWP https://msdn.microsoft.com/en-us/library/windows/desktop/dd371455(v=vs.85).aspx 100%兼容