function TAudioVolume.SetupDefaultAudioEndpoint(Device_Enumerator: IMMDeviceEnumerator): boolean;
var
HR: HResult;
MDevice: IMMDevice;
hnsRequestedDuration: REFERENCE_TIME;
pwfx: PWAVEFORMATEX;
begin
Result := false;
try
// Get default audio endpoint device with the specified data-flow direction (eRender or eCapture)
// and device role.
HR := Device_Enumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, MDevice);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get DefaultAudioEndpoint Device');
FDefaultDevice := GetDeviceInfo(MDevice);
Device := MDevice;
// Get the session manager for the endpoint device.
HR := Device.Activate(IID_IAudioSessionManager, CLSCTX_INPROC_SERVER,
nil, IUnknown
(SessionManager));
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get AudioSessionManager Interface');
// **** Get the IChannelAudioVolume interface :
HR := Device.Activate(IID_IAudioClient, CLSCTX_ALL,
nil, IUnknown(AudioClient));
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get AudioClient Interface');
hnsRequestedDuration := REFTIMES_PER_SEC;
HR := AudioClient.GetMixFormat(pwfx);
if HR <> S_OK
then
raise Exception.Create(
'
Error : Unable to retrieves the stream format that the audio engine uses');
HR := AudioClient.Initialize(AUDCLNT_SHAREMODE_SHARED, 0, hnsRequestedDuration, 0, pwfx,
nil);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to initialize AudioClient Interface');
HR := AudioClient.GetService(IID_IChannelAudioVolume, IUnknown(ChannelAudioVolume));
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get ChannelAudioVolume Interface');
// get SimpleAudioVolume
HR := SessionManager.GetSimpleAudioVolume(
nil, 0, SimpleAudioVolume);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get SimpleAudioVolume Interface');
// Get the control interface for the process-specific audio session.
HR := SessionManager.GetAudioSessionControl(
nil, false, SessionControl);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get AudioSessionControl');
// Register IAudioSessionEvents interface to receive notifications when session events occur.
HR := SessionControl.RegisterAudioSessionNotification(self);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to Register Session Notification Event');
// We can get session enumerator if we use Windows 7 or newer system.
if (Win32MajorVersion > 6)
or ((Win32MajorVersion = 6)
and (Win32MinorVersion >= 1))
then
begin
// Get the session manager2 for the endpoint device.
HR := Device.Activate(IID_IAudioSessionManager2, CLSCTX_ALL,
nil, IUnknown(SessionManager2));
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get AudioSessionManager2 Interface');
// Register TAudioSessionNotification to receive a notification when a session is created.
HR := SessionManager2.RegisterSessionNotification(self);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to Register Session Notification');
// Get the session enumerator
HR := SessionManager2.GetSessionEnumerator(SessionEnumerator);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get SessionEnumerator Interface');
end;
Result := true;
finally
end;
end;