IAudioSessionControl::RegisterAudioSessionNotification 方法 (audiopolicy.h)

RegisterAudioSessionNotification 方法會註冊用戶端以接收會話事件的通知,包括數據流狀態的變更。

語法

HRESULT RegisterAudioSessionNotification(
  [in] IAudioSessionEvents *NewNotifications
);

參數

[in] NewNotifications

用戶端實作 的 IAudioSessionEvents 介面指標 。 如果方法成功,它會在用戶端的 IAudioSessionEvents 介面上呼叫 AddRef 方法。

傳回值

如果方法成功,它會傳回 S_OK。 如果失敗,可能的傳回碼包括,但不限於下表所示的值。

傳回碼 Description
E_POINTER
參數 NewNotificationsNULL
AUDCLNT_E_DEVICE_INVALIDATED
音訊端點裝置已解除叢集,或音訊硬體或相關聯的硬體資源已重新設定、停用、移除,否則無法使用。
AUDCLNT_E_SERVICE_NOT_RUNNING
Windows 音訊服務未執行。

備註

這個方法會將客戶端實作 的 IAudioSessionEvents 介面傳遞至會話管理員。 在成功呼叫此方法之後,會話管理員會呼叫 IAudioSessionEvents 介面中的方法,以通知用戶端各種會話事件。 透過這些方法,用戶端會收到下列會話相關事件的通知:

  • 顯示名稱變更
  • 磁碟區層級變更
  • 會話狀態 (非作用中變更為作用中,或非作用中變更為非作用中)
  • 群組參數變更
  • 使用者移除音訊端點裝置、關閉工作階段管理員或變更串流格式) 所造成的會話中斷連線 (
不再需要通知時,用戶端可以呼叫 IAudioSessionControl::UnregisterAudioSessionNotification 方法來終止通知。

在用戶端釋放 其 IAudioSessionEvents 介面的最終參考之前,它應該呼叫 UnregisterAudioSessionNotification 來取消註冊介面。 否則,應用程式會洩漏 IAudioSessionEventsIAudioSessionControl 物件所持有的資源。 請注意, RegisterAudioSessionNotification 會呼叫用戶端的 IAudioSessionEvents::AddRef 方法,而 UnregisterAudioSessionNotification 會呼叫 IAudioSessionEvents::Release 方法。 如果用戶端在呼叫 UnregisterAudioSessionNotification 之前釋放其 IAudioSessionEvents 介面的參考,會話管理員永遠不會釋放其 IAudioSessionEvents 介面的參考。 例如,設計不佳的 IAudioSessionEvents 實作可能會從 IAudioSessionEvents 物件的解構函式呼叫 UnregisterAudioSessionNotification。 在此情況下,用戶端不會呼叫 UnregisterAudioSessionNotification ,直到會話管理員釋放對 IAudioSessionEvents 介面的參考,而會話管理員將不會釋放其 IAudioSessionEvents 介面的參考,直到用戶端呼叫 UnregisterAudioSessionNotification 為止。 如需 AddRefRelease 方法的詳細資訊,請參閱 Windows SDK 檔中的 IUnknown 介面討論。

此外,客戶端應該先呼叫 UnregisterAudioSessionNotification,再釋放其 IAudioSessionControl 和 IAudioSessionManager 物件的所有參考。 除非用戶端保留至少這兩個物件之一的參考,否則會話管理員會洩漏其配置來保存註冊資訊的記憶體。 註冊通知介面之後,只要至少有這兩個物件的其中一個存在,用戶端才會繼續接收通知。

如需呼叫 RegisterAudioSessionNotification 方法的程式代碼範例,請參閱 舊版音訊應用程式的音訊事件

規格需求

需求
最低支援的用戶端 Windows Vista [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2008 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 audiopolicy.h

另請參閱

IAudioSessionControl 介面

IAudioSessionControl::UnregisterAudioSessionNotification

IAudioSessionEvents 介面

IAudioSessionManager