go语言让windows发出声音,或者播放音乐

wangqishu · · 7153 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

                    go语言让windows发出声音,或者播放音乐的例子:会发出alert警告的声音

 

                    ( 这是我应群员的求助写的, 如果你需要了解其中的调用原理或过程 或更多go语言调用win32api的资料,加群: 218160862 , 答案我告诉你)

 

                           package main

                            func main(){ 

                                     winSound()

                            }

 

                            // golang 让windows发出警告的声音  todo 需要完善播放mp3之类

                            func winSound(  )  {

                                     funInDllFile, err := syscall.LoadLibrary("Winmm.dll") // 调用的dll文件

                                     if err != nil {

                                               print("cant not call : syscall.LoadLibrary , errorInfo :" + err.Error())

                                     }

                                     defer syscall.FreeLibrary(funInDllFile)

                                     // 调用的dll里面的函数是:

                                     funName := "PlaySound"

 

                                     // 注册一长串调用代码,简化为 _win32Fun 变量.

                                     win32Fun, err := syscall.GetProcAddress(syscall.Handle(funInDllFile), funName) 

 

                                     // 通过syscall.Syscall6()去调用win32的xxx函数,因为xxx函数有3个参数,故需取Syscall6才能放得下. 最后的3个参数,设置为0即可

                                     _, _, err = syscall.Syscall6(

                                               uintptr(win32Fun),                                                                             // 调用的函数名

                                               3,                                                                                                     // 指明该函数的参数数量

                                              

                                               uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("alert") ) ),                    // 该函数的参数1. 可通过msdn查找函数名 查参数含义

                                               // SystemStart

                                               uintptr( 0 ),                                                                                       // 该函数的参数2.                                  

                                               uintptr( 0 ),                                                                                       // 该函数的参数3.               

                                                                                            

                                               0,                                                   

                                               0,                                                   

                                               0 )             

                            }


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:wangqishu

查看原文:go语言让windows发出声音,或者播放音乐

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

7153 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传