音频文件从自身资源读取源码.ahk

; 用U32编译此脚本后自身读取播放,用U64编译需要拷贝一份再读取才行,无法读取自身后播放
SetWorkingDir %A_ScriptDir%

编译打包音频文件() {  ; 打包并不释放
  FileInstall, zapmachine.wav, -
}

; U32编译可以读取自身资源文件并播放
hModule := DllCall("LoadLibraryEx", "Str", A_ScriptFullPath, "Ptr", 0, "Int", 0x02, "Ptr")
; hModule := DllCall( "GetModuleHandle", "UInt", 0, "Ptr" )  ; 获取自身的句柄【类似上面那句】

; U64编译可以获取自身文件,但无法播放提取文件。以下是处理方法
; FileCopy, % A_ScriptFullPath, % A_Temp "\" A_ScriptName, 1
; hModule := DllCall("LoadLibraryEx", "Str", A_Temp "\" A_ScriptName, "Ptr", 0, "Int", 0x02, "Ptr")

Size := ResReadEx( Sound_JUMP, hModule, "ZAPMACHINE.WAV" )  ; 获取资源文件的WAV

; FileRead, Sound_JUMP, *c ZAPMACHINE.wav

MsgBox % "文件大小:" Size "`n资源指针:" &Sound_JUMP

PlaySoundAsync( Sound_JUMP )  ; 播放WAV

DllCall("FreeLibrary", "Ptr", hModule)

Sleep 3000
Return

; ResRead() By SKAN, http://www.autohotkey.com/board/topic/57631-crazy-scripting-resource-only-dll-for-dummies-36l-v07/?p=609282
ResReadEx( ByRef Var, hModule, Key ) { 
  VarSetCapacity( Var, 128 ), VarSetCapacity( Var, 0 )

  If hRes := DllCall( "FindResource", "Ptr", hModule, "Str", Key, "UInt", 10, "Ptr" )
    If hData := DllCall( "LoadResource", "Ptr", hModule, "Ptr", hRes, "Ptr" )
      If pData := DllCall( "LockResource", "Ptr", hData )
        Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", "Ptr", hModule, "Ptr", hRes, "Ptr" ) )
              ,  DllCall( "RtlMoveMemory", "Str", Var, "Ptr", pData, "UInt", nSize )
  Return 0
}

PlaySound( ByRef Sound ) {
  Return DllCall( "winmm.dll\PlaySound", "UInt", &Sound, "UInt", 0, "UInt", 0x6 ) ; SND_MEMORY := 0x4 | SND_NODEFAULT := 0x2
}

PlaySoundAsync( ByRef Sound ) { ; http://www.autohotkey.com/board/topic/96484-read-wave-resource-from-exe-using-skans-dllread-possible/?p=609800
  Return DllCall( "winmm.dll\PlaySound" ( A_IsUnicode ? "W" : "A" ), "Ptr", &Sound, "UInt", 0, "UInt", 0x7)
}

 

音频外部读取.ahk

#NoEnv
SetWorkingDir %A_ScriptDir%

ResReadEx( Sound_JUMP,  "zapmachine.wav" )
;ResReadEx( Sound_GAMEOVER, hModule, "GAMEOVER.WAV" )

PlaySoundAsync( Sound_JUMP )
;PlaySound( Sound_JUMP )
;PlaySound( Sound_GAMEOVER )

Sleep 5000

; ResRead() By SKAN, from http://www.autohotkey.com/board/topic/57631-crazy-scripting-resource-only-dll-for-dummies-36l-v07/?p=609282
ResReadEx( ByRef Var, Key ) { 
  VarSetCapacity( Var, 128 ), VarSetCapacity( Var, 0 )
  If ! ( A_IsCompiled ) {
    FileGetSize, nSize, %Key%
    FileRead, Var, *c %Key%
    Return nSize
  }
 
  If hRes := DllCall( "FindResource", "UInt", hModule, "Str", Key, "UInt", 10, "Ptr" )
    If hData := DllCall( "LoadResource", "UInt", hModule, "UInt", hRes, "Ptr" )
      If pData := DllCall( "LockResource", "UInt", hData )
        Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", "UInt", hModule, "UInt", hRes, "Ptr" ) )
              ,  DllCall( "RtlMoveMemory", "Str", Var, "UInt", pData, "UInt", nSize )
  Return 0  
}

PlaySoundAsync( ByRef Sound ) {
  Return DllCall( "winmm.dll\PlaySound" ( A_IsUnicode ? "W" : "A" ), "UInt", &Sound, "UInt", 0, "UInt", 0x7 )
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。