IAudioViewManagerService::SetAudioStreamWindow method (audioclient.h)

Associates the specified HWND window handle with an audio stream.

Syntax

HRESULT SetAudioStreamWindow(
  HWND hwnd
);

Parameters

hwnd

The HWND with which the audio stream wll be associated.

Remarks

An app can choose to associate audio streams with a particular window of their app for proper audio location representation in a Mixed Reality scenario

Get an instance of the IAudioViewManagerService by calling GetService on the IAudioClient instance representing the stream you want to associate a window with. The following code example illustrates creating an audio stream on the default audio render endpoint and associating it with an HWND.

#include <audioclient.h>

HRESULT CreateAudioStreamAndAttachToHwnd(_In_ HWND hwnd, _Out_ IAudioClient **audioStream)
{

    wil::com_ptr_nothrow<IMMDeviceEnumerator> enumerator;
    RETURN_IF_FAILED(CoCreateInstance(__uuidof(IMMDeviceEnumerator),
    NULL,
    CLSCTX_ALL,
    IID_PPV_ARGS(&enumerator)));
    
    wil::com_ptr_nothrow<IMMDevice> device;
    RETURN_IF_FAILED(enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device));
    
    wil::com_ptr_nothrow<IAudioClient> audioClient;
    RETURN_IF_FAILED(device->Activate(__uuidof(IAudioClient),
    CLSCTX_ALL,
    NULL,
    (void**)&audioClient));
    
    wil::unique_cotaskmem_ptr<WAVEFORMATEX> wfx;
    RETURN_IF_FAILED(audioClient->GetMixFormat(wil::out_param_ptr<WAVEFORMATEX**>(wfx)));
    
    constexpr REFERENCE_TIME hnsRequestedDuration = 10000000;
    RETURN_IF_FAILED(audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
    0,
    hnsRequestedDuration,
    0,
    wfx.get(),
    NULL));
    
    wil::com_ptr_nothrow<IAudioViewManagerService> audioViewManagerService;
    RETURN_IF_FAILED(audioClient->GetService(IID_PPV_ARGS(&audioViewManagerService)));
    RETURN_IF_FAILED(audioViewManagerService->SetAudioStreamWindow(hwnd));
    
    *audioStream = spAudioClient.detach();
    
    return S_OK;
}

Requirements

Requirement Value
Header audioclient.h