Windows 10 中的 XAudio 2.8 虚拟语音迁移使其不太常见,但您仍然需要处理OnCriticalError
场景。通常,只要将新的音频设备添加到系统中,您就会尝试重置语音。
在 Win32 桌面应用程序中:
#include <Dbt.h>
HDEVNOTIFY g_hNewAudio = nullptr;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
case WM_CREATE:
if (!g_hNewAudio)
{
// Ask for notification of new audio devices
DEV_BROADCAST_DEVICEINTERFACE filter = { 0 };
filter.dbcc_size = sizeof(filter);
filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_classguid = KSCATEGORY_AUDIO;
g_hNewAudio = RegisterDeviceNotification(hWnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE);
}
break;
case WM_CLOSE:
if (g_hNewAudio)
{
UnregisterDeviceNotification(g_hNewAudio);
g_hNewAudio = nullptr;
}
DestroyWindow(hWnd);
break;
case WM_DEVICECHANGE:
switch (wParam)
{
case DBT_DEVICEARRIVAL:
{
auto pDev = reinterpret_cast<PDEV_BROADCAST_HDR>(lParam);
if (pDev)
{
if (pDev->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
auto pInter = reinterpret_cast<const PDEV_BROADCAST_DEVICEINTERFACE>(pDev);
if (pInter->dbcc_classguid == KSCATEGORY_AUDIO)
{
if (g_game)
g_game->NewAudioDevice();
}
}
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
{
auto pDev = reinterpret_cast<PDEV_BROADCAST_HDR>(lParam);
if (pDev)
{
if (pDev->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
auto pInter = reinterpret_cast<const PDEV_BROADCAST_DEVICEINTERFACE>(pDev);
if (pInter->dbcc_classguid == KSCATEGORY_AUDIO)
{
if (g_game)
g_game->NewAudioDevice();
}
}
}
}
break;
}
return 0;
在 UWP 应用中,你使用DeviceWatcher
:
Windows::Devices::Enumeration::DeviceWatcher^ m_audioWatcher;
virtual void Initialize(CoreApplicationView^ applicationView)
{
m_audioWatcher = DeviceInformation::CreateWatcher(DeviceClass::AudioRender);
m_audioWatcher->Added += ref new TypedEventHandler<DeviceWatcher^, DeviceInformation^>(this, &ViewProvider::OnAudioDeviceAdded);
m_audioWatcher->Updated += ref new TypedEventHandler<DeviceWatcher^, DeviceInformationUpdate^>(this, &ViewProvider::OnAudioDeviceUpdated);
m_audioWatcher->Start();
}
void OnAudioDeviceAdded(Windows::Devices::Enumeration::DeviceWatcher^ sender, Windows::Devices::Enumeration::DeviceInformation^ args)
{
m_game->NewAudioDevice();
}
void OnAudioDeviceUpdated(Windows::Devices::Enumeration::DeviceWatcher^ sender, Windows::Devices::Enumeration::DeviceInformationUpdate^ args)
{
m_game->NewAudioDevice();
}