3

I try to write some application that will monitor audio sessions (like SndVol does). I activated IAudioSessionManager2, got list of current audio sessions through IAudioSessionEnumerator, registered for the notification using RegisterSessionNotification method in audio session manager. However, my application never receives notification OnSessionCreated(). Of course, I read the similar topics here: Notifications not sent, IAudioSessionNotification anyone have working code

I read carefully all the answers but I couldn't find what I am doing wrong. Here is my code (Delphi):

TDNAudioSessionManager = class(TDNUnknownObject, IAudioSessionNotification)
private
  FManager: IAudioSessionManager2;
  FList: TDNAudioSessionList;
  function IsManagerValid: Boolean;
protected
  procedure ActivateSessionManager(out AManager: IAudioSessionManager2);
  procedure GetCurrentSessions;
  // IAudioSessionNotification
  function OnSessionCreated(ANewSession: IAudioSessionControl): HRESULT; stdcall;
public
  constructor Create;
  destructor Destroy; override;
end;

constructor TDNAudioSessionManager.Create;
begin
  inherited Create;
  ActivateSessionManager(FManager);
  if IsManagerValid then
  begin
    FList := TDNAudioSessionList.Create;
    FManager.RegisterSessionNotification(Self);
    GetCurrentSessions;
   end;
 end;

destructor TDNAudioSessionManager.Destroy;
begin
  if IsManagerValid then
  begin
    FManager.UnregisterSessionNotification(Self);
    FManager := nil;
  end;
  FreeAndNil(FList);
  inherited Destroy;
 end;

function TDNAudioSessionManager.IsManagerValid: Boolean;
begin
  Result := Assigned(FManager);
end;

procedure TDNAudioSessionManager.GetCurrentSessions;
var
  AEnumerator: IAudioSessionEnumerator;
  ASession: IAudioSessionControl;
  ACount: Integer;
  I: Integer;
begin
  if IsManagerValid then
    if FManager.GetSessionEnumerator(AEnumerator) = S_OK then
    try
      AEnumerator.GetCount(ACount);
      for I := 0 to ACount - 1 do
      begin
        AEnumerator.GetSession(I, ASession);
        FList.Add(ASession);
      end;
    finally
      AEnumerator := nil;
    end;
end;

function TDNAudioSessionManager.OnSessionCreated(ANewSession: IAudioSessionControl): HRESULT;
begin
  FList.Add(ANewSession);
  Result := S_OK;
end;

procedure TDNAudioSessionManager.ActivateSessionManager(out AManager: IAudioSessionManager2);
var
  AEnumerator: IMMDeviceEnumerator;
  ADefault: IMMDevice;
  ARes: HRESULT;
begin
  AManager := nil;
  ARes := CoCreateInstance(CLSID_MMDeviceEnumerator, nil, CLSCTX_INPROC_SERVER, IID_IMMDeviceEnumerator, AEnumerator);
  if not ARes = S_OK then Exit;
  if AEnumerator.GetDefaultAudioEndpoint(eRender, eConsole,  ADefault) = S_OK then
  try
    ADefault.Activate(IID_IAudioSessionManager2, CLSCTX_INPROC_SERVER, nil, AManager);
  finally
    ADefault := nil;
  end;
end;

initialization
  CoInitializeEx(nil, COINIT_MULTITHREADED);
end.

Note: TDNUnknownObject is class that implements IUnknown methods.

And one more question: what event does audio session send when application is closed? (in SndVol it is removed from list). I tried OnSessionDisconnected, OnStateChanged (with state AudioSessionExpired) but my app never receive them too.

Thanks in advance!

TDNUnknownObject:

TDNUnknownObject = class(TObject, IUnknown)
protected
  // IUnknown
  function _AddRef: Integer; stdcall;
  function _Release: Integer; stdcall;
  function QueryInterface(const IID: TGUID; out Obj): HRESULT; virtual; stdcall;
end;

function TDNUnknownObject._AddRef: Integer; stdcall;
begin
  Result := -1;
end;

function TDNUnknownObject._Release: Integer; stdcall;
begin
  Result := -1;
end;

function TDNUnknownObject.QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
begin
  if GetInterface(IID, Obj) then
    Result := S_OK
  else
    Result := E_NOINTERFACE;
end;
4

1 回答 1

-1

好吧,我对此摸不着头脑。第一个问题出现了:为什么不使用 TInterfacedObject?节省了大量工作,并被证明可以正常工作。此对象派生自 IUnknown 并别名为 IInterface,并且完全按照您在此处尝试执行的操作。当然,您可以尝试编写自己的版本,但要注意:Delphi 编译器会更加严格,因此您很容易遇到双重实现和名称冲突等陷阱。由于这些原因,编译器、链接器和调试器的行为可能会很奇怪。

好的,来自 MS 的简短说明:确保应用程序通过在非 UI 线程中调用 CoInitializeEx(Nil, COINIT_MULTITHREADED) 使用多线程单元 (MTA) 模型初始化 COM。如果 MTA 未初始化,则应用程序不会从会话管理器接收会话通知。运行应用程序用户界面的线程应该是初始化单元线程模型。

你去:你的初始化部分不正确。它应该是:

initialization
  CoInitializeEx(Nil,
                 COINIT_APARTMENTTHREADED);
于 2016-08-21T21:06:35.660 回答