> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XUserGetDefaultAudioEndpointUtf16

> XUserGetDefaultAudioEndpointUtf16

# XUserGetDefaultAudioEndpointUtf16

擷取特定使用者的預設音訊端點。

## 語法

```cpp theme={null}
HRESULT XUserGetDefaultAudioEndpointUtf16(  
         XUserLocalId user,  
         XUserDefaultAudioEndpointKind defaultAudioEndpointKind,  
         size_t endpointIdUtf16Count,  
         wchar_t* endpointIdUtf16,  
         size_t* endpointIdUtf16Used  
)  
```

### 參數

*user*   \
類型：[XUserLocalId](/zh-TW/reference/system/xuser/structs/xuserlocalid)\
要取得其預設音訊端點之使用者的本機識別碼。

*defaultAudioEndpointKind*   \
類型：[XUserDefaultAudioEndpointKind](/zh-TW/reference/system/xuser/enums/xuserdefaultaudioendpointkind)\
列舉值，指定使用者的預設音訊端點是轉譯還是擷取音訊。

*endpointIdUtf16Count*   \
類型：size\_t\
*endpointIdUtf16* 參數所指向之緩衝區的 wchar\_t 計數。請傳遞 **XUserAudioEndpointMaxUtf16Count**。

*endpointIdUtf16*   \_Out\_writes\_to\_(endpointIdUtf16Count,*endpointIdUtf16Used)\
類型：wchar\_t*\
包含使用者預設音訊端點的識別碼。

*endpointIdUtf16Used*   \_Out\_opt\_\
類型：size\_t\*

包含 *endpointIdUtf16* 參數所傳回之識別碼的實際 wchar\_t 計數。

### 傳回值

類型：HRESULT

如果成功，則傳回 S\_OK；否則傳回錯誤碼。
如需錯誤碼清單，請參閱[錯誤碼](/zh-TW/reference/errorcodes)。

## 備註

<Note>在時間敏感執行緒上呼叫此函式並不安全。如需詳細資訊，請參閱[時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

XUserGetDefaultAudioEndpointUtf16 函式會接受 [XUserDefaultAudioEndpointKind](/zh-TW/reference/system/xuser/enums/xuserdefaultaudioendpointkind) 列舉作為引數。

當特定使用者的預設音訊端點變更時，會叫用 [XUserDefaultAudioEndpointUtf16ChangedCallback](/zh-TW/reference/system/xuser/functions/xuserdefaultaudioendpointutf16changedcallback) 回呼函式。

若要註冊以在預設音訊端點變更時接收回呼，請呼叫 [XUserRegisterForDefaultAudioEndpointUtf16Changed](/zh-TW/reference/system/xuser/functions/xuserregisterfordefaultaudioendpointutf16changed)。

若要取消註冊在預設音訊端點變更時接收回呼，請呼叫 [XUserUnregisterForDefaultAudioEndpointUtf16Changed](/zh-TW/reference/system/xuser/functions/xuserunregisterfordefaultaudioendpointutf16changed)。

下列範例示範如何擷取使用者的預設音訊端點。

```cpp theme={null}
HRESULT GetAudioDeviceAssociation(
    IMMDeviceEnumerator* audioDeviceEnumerator,
    XUserLocalId user,
    XUserDefaultAudioEndpointKind defaultAudioEndpointKind,
    _Outptr_result_maybenull_ IMMDevice** endpoint)
{
    wchar_t audioDeviceId[XUserAudioEndpointMaxUtf16Count];
    RETURN_IF_FAILED_WITH_EXPECTED(XUserGetDefaultAudioEndpointUtf16(user, defaultAudioEndpointKind, std::size(audioDeviceId), audioDeviceId, nullptr), E_NOTFOUND);

    return audioDeviceEnumerator->GetDevice(audioDeviceId, endpoint);
}

HRESULT GetAudioAssociations()
{
    Mwrl::ComPtr<IMMDeviceEnumerator> audioDeviceEnumerator;

    RETURN_IF_FAILED(CoCreateInstance(
        __uuidof(MMDeviceEnumerator),
        nullptr,
        CLSCTX_ALL,
        __uuidof(IMMDeviceEnumerator),
        (void**)&audioDeviceEnumerator));

    XUserLocalId userLocalId;
    RETURN_IF_FAILED(XUserGetLocalId(_handle.get(), &userLocalId));

    {
        wil::unique_cotaskmem_string id;
        Mwrl::ComPtr<IMMDevice> device;
        if (SUCCEEDED(GetAudioDeviceAssociation(audioDeviceEnumerator.Get(), userLocalId, XUserDefaultAudioEndpointKind::CommunicationRender, &device)))
        {
            RETURN_IF_FAILED(device->GetId(&id));
        }
        appLog.AddLog("Preferred render communication device id: %S\n", id.get());
    }

    {
        wil::unique_cotaskmem_string id;
        Mwrl::ComPtr<IMMDevice> device;
        if (SUCCEEDED(GetAudioDeviceAssociation(audioDeviceEnumerator.Get(), userLocalId, XUserDefaultAudioEndpointKind::CommunicationCapture, &device)))
        {
            RETURN_IF_FAILED(device->GetId(&id));
        }
        appLog.AddLog("Preferred capture communication device id: %S\n", id.get());
    }

    return S_OK;
}
```

## 需求

**標頭：** XUser.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

[XUser](/zh-TW/reference/system/xuser/xuser_members)

[XUserDefaultAudioEndpointUtf16ChangedCallback](/zh-TW/reference/system/xuser/functions/xuserdefaultaudioendpointutf16changedcallback)

[XUserRegisterForDefaultAudioEndpointUtf16Changed](/zh-TW/reference/system/xuser/functions/xuserregisterfordefaultaudioendpointutf16changed)

[XUserUnregisterForDefaultAudioEndpointUtf16Changed](/zh-TW/reference/system/xuser/functions/xuserunregisterfordefaultaudioendpointutf16changed)


## Related topics

- [XUserGetDefaultAudioEndpointUtf16](/reference/system/xuser/functions/xusergetdefaultaudioendpointutf16.md)
- [XUserDefaultAudioEndpointKind](/zh-CN/reference/system/xuser/enums/xuserdefaultaudioendpointkind.md)
