admin 管理员组文章数量: 1184232
PlaySound 代码如下
#include <Windows.h>
#include <mmsystem.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#pragma comment(lib,"WinMM.Lib")
using namespace std;
BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
{
// Get the required size of the buffer that receives the Unicode
// string.
DWORD dwMinSize;
dwMinSize = MultiByteToWideChar(CP_ACP, 0, lpcszStr, -1, NULL, 0);
assert(dwSize >= dwMinSize);
// Convert headers from ASCII to Unicode.
MultiByteToWideChar(CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
return TRUE;
}
int main(int argc, char** argv)
{
string s = "D:\\MyDesktop\\6.wav";// "bt.wav";
wchar_t wText[30] = { 0 };
MByteToWChar(s.c_str(), wText, sizeof(wText) / sizeof(wText[0]));
PlaySound(wText, NULL, SND_FILENAME | SND_SYNC);
system("pause");
return 0;
}
如果要停止当前的音乐,将PlaySound的第一个参数改为NULL即可。PlaySound(NULL, NULL, SND_FILENAME | SND_SYNC);
缺点
只能播放wav文件,对于其他的文件,需要寻找其他方法。
关于字符数组和LPWSTR类型数据转化的方法
- http://cppblog/sunraiing9/archive/2007/03/21/20281.html
版权声明:本文标题:C++在windows下播放wav音频文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1767571899a3483384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论