마음의 안정을 찾기 위하여 - [Delphi] 이어폰 연결 감지
2371821
429
515
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
[Delphi] 이어폰 연결 감지
Delphi/시스템 정보 | 2024/07/04 10:10



uses

  Windows, MMDeviceApi, ComObj, ActiveX, Classes, SysUtils;



type

  TForm1 = class(TForm)

  private

    FDeviceEnumerator: IMMDeviceEnumerator;

    FEndpointVolume: IAudioEndpointVolume;

    FHeadphoneConnected: Boolean;

    procedure CheckHeadphoneStatus;

  public

    constructor Create(AOwner: TComponent); override;

    destructor Destroy; override;

  end;



implementation



constructor TForm1.Create(AOwner: TComponent);

begin

  inherited Create(AOwner);

  CoInitialize(nil);

  FDeviceEnumerator := CreateComObject(CLASS_MMDeviceEnumerator) as IMMDeviceEnumerator;

  CheckHeadphoneStatus;

end;



destructor TForm1.Destroy;

begin

  FDeviceEnumerator := nil;

  FEndpointVolume := nil;

  CoUninitialize;

  inherited Destroy;

end;



procedure TForm1.CheckHeadphoneStatus;

var

  Device: IMMDevice;

  State: DWORD;

begin

  FDeviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, Device);

  if Assigned(Device) then

  begin

    Device.GetState(State);

    if State = DEVICE_STATE_ACTIVE then

      FHeadphoneConnected := True

    else

      FHeadphoneConnected := False;



    // 여기에서 이어폰 연결 상태에 따른 추가 작업을 수행할 수 있습니다.

    if FHeadphoneConnected then

      ShowMessage('Headphones are connected')

    else

      ShowMessage('Headphones are not connected');

  end;

end;



end.

DirectX를 이용한 방법
uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, DirectSound;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Label1: TLabel;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

    FDirectSound: IDirectSound;

    function IsHeadphonesConnected: Boolean;

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);

begin

  if IsHeadphonesConnected then

    Label1.Caption := 'Headphones are connected'

  else

    Label1.Caption := 'Headphones are not connected';

end;



function TForm1.IsHeadphonesConnected: Boolean;

var

  DSCaps: TDSCAPS;

begin

  Result := False;

  

  if DirectSoundCreate(nil, FDirectSound, nil) = DS_OK then

  begin

    ZeroMemory(@DSCaps, SizeOf(DSCaps));

    DSCaps.dwSize := SizeOf(DSCaps);

    

    if FDirectSound.GetCaps(DSCaps) = DS_OK then

    begin

      if (DSCaps.dwFlags and DSCAPS_PRIMARYMONO <> 0) or

         (DSCaps.dwFlags and DSCAPS_PRIMARYSTEREO <> 0) then

      begin

        Result := True; // Assume headphones are connected if a primary sound device is available

      end;

    end;

    

    FDirectSound := nil;

  end;

end;



end.
2024/07/04 10:10 2024/07/04 10:10
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1][2][3][4][5][6][7][8] ... [1323] :
«   2024/10   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
전체 (1323)
출판 준비 (0)
My-Pro... (41)
사는 ... (933)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (6)
Database (12)
리눅스 (29)
Windows (25)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (21)
잡다한... (7)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)