SetBatchLines -1
#SingleInstance Force

Loop 
  ToolTip % "线程占用测试:" n := n="" ? 1 : ++n
Return

; 由于调用是系统API播放,会对MP3文件规格有所限制。wav标准就不存在这种问题
F1::
  缓存上次路径 := 音频文件路径
  FileSelectFile, 音频文件路径, , ,选择音频文件, 文件(*.wav; *.mp3)
  if (音频文件路径="")
    Return
  停止播放(缓存上次路径)
  异步播放音频(音频文件路径)
Return

F2::暂停播放(音频文件路径)

F3::继续播放(音频文件路径)

F4::
  音频文件长度 := GetAudioDuration(音频文件路径)
  MsgBox % 音频长度时间换算(音频文件长度)
Return

; 此api不能播放的,可以用WMPlayer.OCX正常调用
异步播放音频(mFile) {
  if !(DN := GetAudioDuration(mFile))
    MsgBox 0x10, 不支持此音频文件, 系统API不支持此音频播放,`n可转换成恒定码率再试一次。, 2
  DllCall("Winmm\mciSendString", "Str", "Open """ mFile """", "Uint", 0, "Uint", 0, "Uint", 0)
  , DllCall("Winmm\mciSendString", "Str", "Play """ mFile """ FROM 000 to " DN, "Uint", 0, "Uint", 0, "Uint", 0)
}

继续播放(mFile) {
  DllCall("Winmm\mciSendString", "Str", "Play """ mFile """", "Uint", 0, "Uint", 0, "Uint", 0)
}

暂停播放(mFile) {
  DllCall("Winmm\mciSendString", "Str", "Pause """ mFile """", "Uint", 0, "Uint", 0, "Uint", 0)
}

停止播放(mFile) {
  DllCall("Winmm\mciSendString", "Str", "Close """ mFile """", "Uint", 0, "Uint", 0, "Uint", 0)
}

; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=40538
音频长度时间换算(DN) {
  DURATION_IN_SECONDS := Floor(DN / 1000)
  ((DURATION_IN_SECONDS >= 3600) ? (HOURS := Floor(DURATION_IN_SECONDS / 3600)) : (HOURS := 00))
  ((DURATION_IN_SECONDS - (HOURS * 3600) > 0) ? (MINUTES := Floor((DURATION_IN_SECONDS - (HOURS * 3600)) / 60)) : (MINUTES := 00))
  MINUTES := SubStr(00 . MINUTES, -1, 2)
  ((DURATION_IN_SECONDS - (HOURS * 3600) - (MINUTES * 60) > 0) ? (SECONDS := Floor(DURATION_IN_SECONDS - (HOURS * 3600) - (MINUTES * 60))) : (SECONDS := 00))
  SECONDS := SubStr(00 . SECONDS, -1, 2)
  Return HOURS . ":" . MINUTES . ":" . SECONDS
}

; 获取音频文件长度 By SKAN  https://www.autohotkey.com/forum/viewtopic.php?p=361791#361791
GetAudioDuration(mFile) {
  VarSetCapacity(DN, 16), DLLFunc := "winmm.dll\mciSendString" (A_IsUnicode ? "W" : "A")
  , DllCall(DLLFunc, "Str", "Open """ mFile """ Alias MP3", "Uint", 0, "Uint", 0, "Uint", 0)
  , DllCall(DLLFunc, "Str", "Status MP3 Length", "Str", DN, "Uint", 16, "Uint", 0)
  , DllCall(DLLFunc, "Str", "Close MP3", "Uint", 0, "Uint", 0, "Uint",0)
  Return DN
}

; https://docs.microsoft.com/zh-CN/previous-versions//dd743680(v=vs.85)

/*
; 指定4播放内存文件, 指定1异步播放
FileRead, memWav, *c C:\Windows\Media\Windows Ding.wav

DllCall("Winmm\PlaySound", "ptr", &memWav, "ptr", 0, "uint", 4 | 1)
Sleep, 1500
DllCall("Winmm\PlaySound", "ptr", &memWav, "ptr", 0, "uint", 4 | 1)
Sleep, 1500
*/

 

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