如何使用Windows API在C ++中PlaySound? [英] How to PlaySound in C++ using Windows API?

查看:284
本文介绍了如何使用Windows API在C ++中PlaySound?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的编码中播放音乐文件,但失败。我有我的音乐文件在同一个文件夹保存.cpp文件。

I try to play a music file in my coding, but failed. I have my music file in the same folder which save .cpp file.

有人可以帮助我吗?

我的代码是:

#include <iostream>  
#include <windows.h>

int main() { 
    PlaySound("kenny g.WAV", NULL, SND_ASYNC);    
}


推荐答案

路径,请确保您发送的文件名(使用SND_FILENAME标志),并暂停程序足够长的时间播放声音文件(例如,使用getchar())。您需要在项目设置中链接winmm.lib库,并在标题中包含#include windows.h和mmsystem.h。

You need to use the absolute path, make sure that you're sending a filename (use SND_FILENAME flag), and pause the program long enough to play the sound file (e.g., use getchar()). You need to link the winmm.lib library in your project settings, and #include windows.h and mmsystem.h in the header.

#include <windows.h>
#include <mmsystem.h>

int main() {
    PlaySoundA((LPCSTR) "C:\\kenny g.WAV", NULL, SND_FILENAME | SND_ASYNC);
    getchar();
}

API: http://msdn.microsoft.com/en-us/library/ms712879(VS.85).aspx

这应该是。让我知道,谢谢!

API: http://msdn.microsoft.com/en-us/library/ms712879(VS.85).aspx
That should be it. Let me know, thanks!

这篇关于如何使用Windows API在C ++中PlaySound?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆