找回密码
 加入
搜索
查看: 4343|回复: 5

[AU3基础] 检查耳机是否已接入

  [复制链接]
发表于 2016-3-26 10:48:34 | 显示全部楼层 |阅读模式
悬赏4金钱已解决
测试LOOP前检查耳机是否已插入,如量没有则提示插入,耳机芯片Realtek,耳机接入后,在Realtek高清晰音频管理器里耳机接口变亮,否则变暗;

最佳答案

查看完整内容

[au3] ;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], ...
发表于 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[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

所需库文件 http://www.signa5.com/autoit/BASS/BASS.zip
not test!!
发表于 2016-3-26 15:35:10 | 显示全部楼层
回复 1# shaw_518

看来是同一领域的人,Realtek不是有工具可以侦测JD吗?
 楼主| 发表于 2016-3-28 08:44:25 | 显示全部楼层
呵呵,,问过供应商了,他们不提供!
发表于 2016-3-28 10:20:37 | 显示全部楼层
似乎VISTA以上的系统可以用IMMNotificationClient::OnDeviceStateChanged method,但要准备自编DLL文件调用了。
 楼主| 发表于 2016-3-29 09:01:46 | 显示全部楼层
回复 2# 虫子樱桃


    缺少BassWASAPI.au3库文件!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-26 10:11 , Processed in 0.086034 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表