AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Projekte Audio Session Test
Thema durchsuchen
Ansicht
Themen-Optionen

Audio Session Test

Ein Thema von EWeiss · begonnen am 25. Okt 2018 · letzter Beitrag vom 28. Okt 2018
Antwort Antwort
EWeiss
(Gast)

n/a Beiträge
 
#1

AW: Audio Session Test

  Alt 28. Okt 2018, 10: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 10:50 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:34 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz