Einzelnen Beitrag anzeigen

EWeiss
(Gast)

n/a Beiträge
 
#6

AW: Audio Session Test

  Alt 28. Okt 2018, 11:47
Zwei Korrekturen..
1. Verhindern das gleiche Anwendungen doppelt oder dreifach angezeigt werden.
Delphi-Quellcode:
procedure TSessionThread.Execute;
var
  Success: HResult;
  Msg: TMsg;
  HR: HResult;
  i: integer;
  IntI: integer;
  M, N: integer;
  bDuplicate: boolean;
  ASessionInfo: TSessionInfo;
begin
  Success := CoInitializeEx(nil, COINIT_MULTITHREADED);

  FWnd := AllocateHWnd(WndProc);

  SessionNotification := TAudioSessionNotification.Create;
  SessionNotification.SetMsgHandle(FWnd);
  SetupDefaultAudioEndpoint;

  HR := SessionEnumerator.GetCount(i);
  if HR = S_OK then
     N := i else N := 0;

  M := 0;
  for i := 0 to (N - 1) do
  begin
    ASessionInfo := GetSessionInfoByIndex(i);
    if ASessionInfo.FileName <> 'then // is a invalid session info ?
    begin
      // Check to prevent duplicate registeration of audio session
      bDuplicate := false;
      for IntI := 0 to (M - 1) do
        if SessionInfo[IntI].PID = ASessionInfo.PID then
        begin
          // for the case of reuse of same PID (but different programs)
          if SessionInfo[IntI].State <> AudioSessionStateExpired then
          begin
            bDuplicate := true;
            break;
          end;
        end;

      if not bDuplicate then
      begin
        inc(M);
        SetLength(SessionInfo, M);
        SessionInfo[M-1] := ASessionInfo;
      end;
    end;
  end;
  FSessionCount := M;

  FWmiAsyncEvent := TWmiAsyncEvent.Create;
  FWmiAsyncEvent.OnProcessTerminated := ProcessTerminated;
  FWmiAsyncEvent.Start;

  try
    while not Terminated do
    begin
      if MsgWaitForMultipleObjects(0, nil^, False, 1000, QS_ALLINPUT) = WAIT_OBJECT_0 then
      begin
        while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
        begin
          TranslateMessage(Msg);
          DispatchMessage(Msg);
        end;
      end;

      if bEnd then
        break;
    end;
  finally
    DeallocateHWnd(FWnd);
  end;

  FWmiAsyncEvent.Free;

  if SessionManager2 <> nil then
    SessionManager2.UnregisterSessionNotification(SessionNotification);

  CoUninit(Success);
end;
2. Den Thread absichern (Thread safe) (TTHread.Queue)
Delphi-Quellcode:
procedure TSessionThread.WndProc(var Msg: TMessage);
var
  i: integer;
  bDuplicate: boolean;
begin
  if Msg.Msg = WM_SessionCreate then
  begin
    ASessionInfo := GetSessionInfo(IAudioSessionControl(Msg.WParam));
    if ASessionInfo.FileName <> 'then // Is a valid audio session ?
    begin
      // Check to prevent duplicate registeration of audio session
      bDuplicate := false;
      for i := 0 to (FSessionCount - 1) do
        if SessionInfo[i].PID = ASessionInfo.PID then
        begin
          // for the case of reuse of same PID (but different programs)
          if SessionInfo[i].State <> AudioSessionStateExpired then
          begin
            bDuplicate := true;
            break;
          end;
        end;

      if not bDuplicate then
      begin
        inc(FSessionCount);
        SetLength(SessionInfo, FSessionCount);
        SessionInfo[FSessionCount-1] := ASessionInfo;
        if Assigned(FOnAudioSessionCreate) then
          TTHread.Queue(nil, // executes a procedure call within the main thread.
          procedure // Unlike Synchronize, execution of the current thread
          begin // is allowed to continue.
            FOnAudioSessionCreate(ASessionInfo, FSessionCount);
          end);
      end;
    end;
  end else
  Msg.Result := DefWindowProc(FWnd, Msg.Msg, Msg.WParam, Msg.LParam);
end;

procedure TSessionThread.ProcessTerminated(ProcessID: Cardinal);
var
  i: integer;
begin
  for i := 0 to (FSessionCount - 1) do
  begin
    if SessionInfo[i].PID = ProcessID then
    begin
      SessionInfo[i].State := AudioSessionStateExpired;
      ASessionInfo := SessionInfo[i];

      if Assigned(FOnAudioSessionExpire) then
        TTHread.Queue(nil, // executes a procedure call within the main thread.
          procedure // Unlike Synchronize, execution of the current thread
          begin // is allowed to continue.
            FOnAudioSessionExpire(ASessionInfo, i);
          end);
      break;
    end;
  end;
end;
gruss

Geändert von EWeiss (28. Okt 2018 um 11:50 Uhr)
  Mit Zitat antworten Zitat