设置 MMDevice WaveFormat
Setting MMDevice WaveFormat
我正在尝试使用此代码设置用于录制音频设备的 WaveFormat "MMDevice" 我正在使用 NAudio:
// Getting The WaveFormat for the device
var value = selectedRecordingDevices.Properties[PropertyKeys.PKEY_AudioEngine_DeviceFormat].Value as byte[];
IntPtr unmanagedPointer = Marshal.AllocHGlobal(value.Length);
Marshal.Copy(value, 0, unmanagedPointer, value.Length);
Marshal.FreeHGlobal(unmanagedPointer);
var waveFormat = WaveFormat.MarshalFromPtr(unmanagedPointer);
// Setting The WaveFormat for the device
WaveFormat w = new WaveFormat(44100, 16, 2);
PropVariant p = new PropVariant();
p.pointerValue = WaveFormatToPointer(w);
selectedRecordingDevices.Properties.SetValue(PropertyKeys.PKEY_AudioEngine_DeviceFormat, p);
public static IntPtr WaveFormatToPointer(WaveFormat format)
{
IntPtr formatPointer = Marshal.AllocHGlobal(Marshal.SizeOf(format));
Marshal.StructureToPtr(format, formatPointer, false);
return formatPointer;
}
我遇到了这个异常:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Source=NAudio
StackTrace:
at NAudio.CoreAudioApi.Interfaces.IPropertyStore.SetValue(PropertyKey& key, PropVariant& value)
at NAudio.CoreAudioApi.PropertyStore.SetValue(PropertyKey key, PropVariant value)
1- 在设置您需要设置 StorageAccessMode 的值之前:
selectedRecordingDevice.GetPropertyInformation(StorageAccessMode.ReadWrite);
所以它看起来像这样:
// Setting The WaveFormat for the device
WaveFormat w = new WaveFormat(44100, 16, 2);
PropVariant p = new PropVariant();
p.pointerValue = WaveFormatToPointer(w);
selectedRecordingDevice.GetPropertyInformation(StorageAccessMode.ReadWrite);
selectedRecordingDevices.Properties.SetValue(PropertyKeys.PKEY_AudioEngine_DeviceFormat, p);
2- 它必须 运行 作为管理员
我正在尝试使用此代码设置用于录制音频设备的 WaveFormat "MMDevice" 我正在使用 NAudio:
// Getting The WaveFormat for the device
var value = selectedRecordingDevices.Properties[PropertyKeys.PKEY_AudioEngine_DeviceFormat].Value as byte[];
IntPtr unmanagedPointer = Marshal.AllocHGlobal(value.Length);
Marshal.Copy(value, 0, unmanagedPointer, value.Length);
Marshal.FreeHGlobal(unmanagedPointer);
var waveFormat = WaveFormat.MarshalFromPtr(unmanagedPointer);
// Setting The WaveFormat for the device
WaveFormat w = new WaveFormat(44100, 16, 2);
PropVariant p = new PropVariant();
p.pointerValue = WaveFormatToPointer(w);
selectedRecordingDevices.Properties.SetValue(PropertyKeys.PKEY_AudioEngine_DeviceFormat, p);
public static IntPtr WaveFormatToPointer(WaveFormat format)
{
IntPtr formatPointer = Marshal.AllocHGlobal(Marshal.SizeOf(format));
Marshal.StructureToPtr(format, formatPointer, false);
return formatPointer;
}
我遇到了这个异常:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Source=NAudio
StackTrace:
at NAudio.CoreAudioApi.Interfaces.IPropertyStore.SetValue(PropertyKey& key, PropVariant& value)
at NAudio.CoreAudioApi.PropertyStore.SetValue(PropertyKey key, PropVariant value)
1- 在设置您需要设置 StorageAccessMode 的值之前:
selectedRecordingDevice.GetPropertyInformation(StorageAccessMode.ReadWrite);
所以它看起来像这样:
// Setting The WaveFormat for the device
WaveFormat w = new WaveFormat(44100, 16, 2);
PropVariant p = new PropVariant();
p.pointerValue = WaveFormatToPointer(w);
selectedRecordingDevice.GetPropertyInformation(StorageAccessMode.ReadWrite);
selectedRecordingDevices.Properties.SetValue(PropertyKeys.PKEY_AudioEngine_DeviceFormat, p);
2- 它必须 运行 作为管理员