#Requires AutoHotkey v2.0
PlayMedia("https://dict.youdao.com/dictvoice?audio=apple")

; By Tebayaki
PlayMedia(uri) {
  DllCall("Combase\RoActivateInstance", "ptr", CreateHString("Windows.Media.Playback.MediaPlayer"), "ptr*", iMediaPlayer := ComValue(13, 0), "HRESULT")
  iUri := CreateUri(uri)
  ComCall(47, iMediaPlayer, "ptr", iUri) ; SetUriSource
  ComCall(45, iMediaPlayer) ; Play
  loop {
    ComCall(12, iMediaPlayer, "uint*", &state := 0) ; CurrentState
  } until state == 3
  loop {
    ComCall(12, iMediaPlayer, "uint*", &state := 0) ; CurrentState
  } until state == 4
}

CreateUri(str) {
  DllCall("ole32\IIDFromString", "str", "{44A9796F-723E-4FDF-A218-033E75B0C084}", "ptr", iid := Buffer(16), "HRESULT")
  DllCall("Combase\RoGetActivationFactory", "ptr", CreateHString("Windows.Foundation.Uri"), "ptr", iid, "ptr*", factory := ComValue(13, 0), "HRESULT")
  ComCall(6, factory, "ptr", CreateHString(str), "ptr*", uri := ComValue(13, 0))
  return uri
}

CreateHString(str) {
  DllCall("Combase\WindowsCreateString", "wstr", str, "uint", StrLen(str), "ptr*", &hString := 0, "HRESULT")
  return { Ptr: hString, __Delete: (_) => DllCall("Combase\WindowsDeleteString", "ptr", _, "HRESULT") }
}

 

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