Sign in to follow this  
Followers 0
Danyfirex

Is possible Detect connection of headphones?

7 posts in this topic

Hi mate. today I got a doubt, about if is possible detect when I put my headphones using autoit?

Share this post


Link to post
Share on other sites



Hi, Danyfirex. For XP I know you could tell from the registry; looking at HKEY_LOCAL_MACHINESYSTEMCurrentControlSetHardware ProfilesCurrentSystemCurrentControlSetEnumHDAUDIO would show a switch between 0x00000001 for headphones and 0x00000004 for speakers. However, I think that method has been abandoned in Win7.

There is also the DirectSound API, which has a Device.SpeakerConfig.Headphone property, if you want to go that way. You'd have to poll the devices and look for a match. I haven't played with this myself, but you might search MSDN for more info. Below is just the first hit I came upon.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb318770(v=vs.85).aspx


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Ok thank you very much. I just want to know If that was Possible.

So I'will Read about you Say.

thank you JLogan3o13

Share this post


Link to post
Share on other sites

Looks like you can do this with the BASSWASAPI add-on BASS_WASAPI_SetNotify function

BASSWASAPI beta

http://www.un4seen.com/forum/?topic=11170

Detect device changes

http://www.un4seen.com/forum/?topic=12218.msg84967;hl=detect#msg84967

BASS Function Library (Sound and Music Functions)


I see fascists...

Share this post


Link to post
Share on other sites

Thank you rover I'll see that.

Share this post


Link to post
Share on other sites

I could have used this a few years ago, and TTBOMK there's nothing like this on the forum,

so my curiosity was piqued to check out the device change notification function in the BASS_WASAPI dll.

It works, but is not *useable run from a script.

Perhaps someone will be inspired to code a DLL in another language to implement this.

Should run on Vista, but only tested on Win7 x64 as a 32bit process (does not run as a 64bit process)

The 64 bit version of the latest dll still won't load, as mentioned by BrettF (DllOpen returns -1)

*The notification callback is run in a new thread and AutoIt is not threadsafe.

The basswasapi.dll needs to be run from an app compiled in another language

with some inter-process communication back to AutoIt scripts.

Detects I/O connect/disconnect, but crashes when inputs connected, a bit more stable when outputs connected/disconnected

+ ! _BASS_WASAPI_Startup   = True @error: 0 @extended code: 0
+ ! _BASS_WASAPI_SetNotify = True @error: -1 @extended code: 0

This device is now the Default Output:
>Device Name: AMD HDMI Output (AMD High Definition Audio Device)
>Device ID : {0.0.0.00000000}.{52012906-567c-4847-a343-1006b8f7da97}
+Device Type: HDMI
-Device Status: 3

This device is now the Default Output:
>Device Name: Speakers (High Definition Audio Device)
>Device ID : {0.0.0.00000000}.{3fb5f448-a7f7-4046-8169-10d6a78daf19}
+Device Type: SPEAKERS
-Device Status: 3

;Get audio I/O device change notifications
;coded by rover 2k12
#AutoIt3Wrapper_UseX64=n
#include "BassWASAPI.au3"
#include "Bass.au3"

OnAutoItExitRegister("_Exit")

Global Const $BASS_WASAPI_NOTIFY_DEFINPUT = 2
Global $aDev[11] = ["NETWORKDEVICE", "SPEAKERS", "LINELEVEL", "HEADPHONES", "MICROPHONE", "HEADSET", "HANDSET", "DIGITAL", "SPDIF", "HDMI", "UNKNOWN"]
Global $iCBProc, $aNotify[3], $bRet, $aRet, $sErr = "+ "

$bRet = _BASS_WASAPI_Startup()
If Not $bRet Then $sErr = "! "
ConsoleWrite($sErr & '! _BASS_WASAPI_Startup   = ' & $bRet & ' @error: ' & @error & '  @extended code: ' & @extended & @CRLF)

$iCBProc = DllCallbackRegister("_WasapiNotifyProc", "none", "dword;dword;int_ptr")

$bRet = _BASS_WASAPI_SetNotify(DllCallbackGetPtr($iCBProc))
If Not $bRet Then $sErr = "! "
ConsoleWrite($sErr & '! _BASS_WASAPI_SetNotify = ' & $bRet & ' @error: ' & @error & ' @extended code: ' & @extended & @CRLF & @CRLF)

While 1
Sleep(1000)
If $aNotify[0] Then
  ;ConsoleWrite("!Notify/Device: " & $aNotify[1] &":"& $aNotify[2] & @LF)
  If $aNotify[1] = $BASS_WASAPI_NOTIFY_DEFINPUT Then ConsoleWrite('This device is now the Default Output: ' & @CRLF)
  $aRet = __BASS_WASAPI_GetdeviceInfo($aNotify[2])
  ;ConsoleWrite('->Error code: ' & @error & @crlf) ;### Debug Console
  If @error Then ContinueLoop
;~   For $i In $aRet
;~  ConsoleWrite("+ " & $i & @LF)
;~   Next
  ConsoleWrite('>Device Name: ' & $aRet[0] & @CRLF)
  ConsoleWrite('>Device ID : ' & $aRet[1] & @CRLF)
  ConsoleWrite('+Device Type: ' & $aDev[$aRet[2]] & @CRLF)
  ConsoleWrite('-Device Status: ' & $aRet[3] & @CRLF & @CRLF) ; 3 = BASS_DEVICE_ENABLED = 1 + BASS_DEVICE_DEFAULT = 2
  $aNotify[0] = 0
EndIf
WEnd

Func _Exit()
_BASS_WASAPI_SetNotify(0) ;send null to cancel callback
DllCallbackFree($iCBProc)
EndFunc   ;==>_Exit

Func _WasapiNotifyProc($Notify, $Device, $User)
;#forceref $Notify, $Device, $User
;callback run in a thread
;can't run functions from here, only check global array in main loop
$aNotify[0] = $User
$aNotify[1] = $Notify
$aNotify[2] = $Device
EndFunc   ;==>_WasapiNotifyProc

Func _BASS_WASAPI_SetNotify($pCB = 0, $iUser = 1)
;rover 2k12
Local $BASSWASAPI_ret_ = DllCall($_ghBassWASAPIDll, "int", "BASS_WASAPI_SetNotify", "ptr", $pCB, "int_ptr", $iUser)
If @error Then Return SetError(1, 1, 0) <> 0
If $BASSWASAPI_ret_[0] = 0 Then Return SetError(_BASS_ErrorGetCode(), 0, 0) <> 0
Return SetError(_BASS_ErrorGetCode(), 0, 1) <> 0
EndFunc   ;==>_BASS_WASAPI_SetNotify
Func __BASS_WASAPI_GetdeviceInfo($Device)
;rover 2k12
;fixed version of _BASS_WASAPI_GetdeviceInfo from BassWASAPI.au3 RC1 by BrettF
Local Const $tagBASS_WASAPI_DEVICEINFO = "ptr name;" & _
   "ptr id;" & _
   "dword type;" & _
   "dword flags;" & _
   "float minperiod;" & _
   "float defperiod;" & _
   "dword mixfreq;" & _
   "dword mixchans;"
Local $aBASS_WASAPI_DEVICEINFO[8]
Local $tBASS_WASAPI_DEVICEINFO = DllStructCreate($tagBASS_WASAPI_DEVICEINFO)
Local $aBASSWASAPI_ret_ = DllCall($_ghBassWASAPIDll, "bool", "BASS_WASAPI_GetDeviceInfo", "dword", $Device, "struct*", $tBASS_WASAPI_DEVICEINFO)
If @error Then Return SetError(1, 1, 0)
If $aBASSWASAPI_ret_[0] = 0 Then Return SetError(_BASS_ErrorGetCode(), 0, 0)
Local $tTxtBuf, $pTxtBuf, $aLen
For $i = 0 To 7
  If $i <= 1 Then
   $pTxtBuf = DllStructGetData($tBASS_WASAPI_DEVICEINFO, $i + 1)
   $aLen = DllCall('kernel32.dll', 'int', 'lstrlenA', 'ptr', $pTxtBuf)
   If @error Then ContinueLoop
   $tTxtBuf = DllStructCreate("char[" & $aLen[0] + 1 & "]", $pTxtBuf)
   $aBASS_WASAPI_DEVICEINFO[$i] = DllStructGetData($tTxtBuf, 1)
  Else
   $aBASS_WASAPI_DEVICEINFO[$i] = DllStructGetData($tBASS_WASAPI_DEVICEINFO, $i + 1)
  EndIf
Next
Return $aBASS_WASAPI_DEVICEINFO
EndFunc   ;==>__BASS_WASAPI_GetdeviceInfo

I see fascists...

Share this post


Link to post
Share on other sites

thank you for your time. that's great. I wanna test it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0