shaw_518 发表于 2016-3-26 10:48:34

检查耳机是否已接入

测试LOOP前检查耳机是否已插入,如量没有则提示插入,耳机芯片Realtek,耳机接入后,在Realtek高清晰音频管理器里耳机接口变亮,否则变暗;

虫子樱桃 发表于 2016-3-26 10:48:35


;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 = ["NETWORKDEVICE", "SPEAKERS", "LINELEVEL", "HEADPHONES", "MICROPHONE", "HEADSET", "HANDSET", "DIGITAL", "SPDIF", "HDMI", "UNKNOWN"]
Global $iCBProc, $aNotify, $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 Then
;ConsoleWrite("!Notify/Device: " & $aNotify &":"& $aNotify & @LF)
If $aNotify = $BASS_WASAPI_NOTIFY_DEFINPUT Then ConsoleWrite('This device is now the Default Output: ' & @CRLF)
$aRet = __BASS_WASAPI_GetdeviceInfo($aNotify)
;ConsoleWrite('->Error code: ' & @error & @crlf) ;### Debug Console
If @error Then ContinueLoop
;~   For $i In $aRet
;~ConsoleWrite("+ " & $i & @LF)
;~   Next
ConsoleWrite('>Device Name: ' & $aRet & @CRLF)
ConsoleWrite('>Device ID : ' & $aRet & @CRLF)
ConsoleWrite('+Device Type: ' & $aDev[$aRet] & @CRLF)
ConsoleWrite('-Device Status: ' & $aRet & @CRLF & @CRLF) ; 3 = BASS_DEVICE_ENABLED = 1 + BASS_DEVICE_DEFAULT = 2
$aNotify = 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 = $User
$aNotify = $Notify
$aNotify = $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 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
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 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 + 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

所需库文件 http://www.signa5.com/autoit/BASS/BASS.zip
not test!!

kemyliu 发表于 2016-3-26 15:35:10

回复 1# shaw_518

看来是同一领域的人,Realtek不是有工具可以侦测JD吗?

shaw_518 发表于 2016-3-28 08:44:25

呵呵,,问过供应商了,他们不提供!

love008 发表于 2016-3-28 10:20:37

似乎VISTA以上的系统可以用IMMNotificationClient::OnDeviceStateChanged method,但要准备自编DLL文件调用了。

shaw_518 发表于 2016-3-29 09:01:46

回复 2# 虫子樱桃


    缺少BassWASAPI.au3库文件!
页: [1]
查看完整版本: 检查耳机是否已接入