PlaySound是Windows平台独有的API函数,仅支持.wav格式的音频文件播放,使用的库文件为winmm.lib,该库文件是Windows自带的库文件,只需要申明即可,不需要手动添加。
#include
#include
#pragma comment(lib, "winmm.lib")
//1) MBCS编码
PlaySound("hello.wav", NULL, SND_SYNC | SND_FILENAME | SND_NOSTOP);
//2) Unicode编码
PlaySound(L"hello.wav", NULL, SND_SYNC | SND_FILENAME | SND_NOSTOP);
//3) 兼容MBCS编码与Unicode编码
PlaySound(TEXT("hello.wav"), NULL, SND_SYNC | SND_FILENAME | SND_NOSTOP);
//1) MBCS编码
PlaySound("hello.wav", NULL, SND_ASYNC | SND_FILENAME | SND_NOSTOP);
//2) Unicode编码
PlaySound(L"hello.wav", NULL, SND_ASYNC | SND_FILENAME | SND_NOSTOP);
//3) 兼容MBCS编码与Unicode编码
PlaySound(TEXT("hello.wav"), NULL, SND_ASYNC | SND_FILENAME | SND_NOSTOP);
这里以一个控制台应用程序DoPlay为例来进行说明,使用的开发工具为 Visual Studio 2005
点击Visual Studio工具栏的[File] --》新建 --》Visual C++ —》Win32 --》Win32控制台应用程序 --》名称:DoPaly
//DoPlay.cpp
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound("hello.wav", NULL, SND_SYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
//DoPlay.cpp
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(L"hello.wav", NULL, SND_SYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
//DoPlay.cpp
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(TEXT("hello.wav"), NULL, SND_SYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
//DoPlay.cpp
#include "stdafx.h"
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound("hello.wav", NULL,SND_ASYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
//DoPlay.cpp
#include "stdafx.h"
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(L"hello.wav", NULL,SND_ASYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
//DoPlay.cpp
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(TEXT("hello.wav"), NULL, SND_ASYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
函数 | 音频格式 |
---|---|
PlaySound() | 仅支持.wav音频格式 |
mciSendString() | 支持mp3、ogg、flac、oga、ac3、aac、wavc等音频格式 |
BOOL mciSendString(“Command1 FILE Command2”,NULL,0,NULL);
形参“Command1 FILE Command2”里的3个变量是逻辑&&与的关系,其可能的组合如下:
2) mciSendString函数需要的头文件如下:
#include
#pragma comment(lib,"winmm.lib")
//DoMusic.cpp
#include "stdafx.h"
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
mciSendString(TEXT("play .\\World.mp3"), NULL, 0, NULL);
system("pause");
return 0;
}
//DoMusic.cpp
#include "stdafx.h"
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
mciSendString(TEXT("play .\\World.mp3 repeat"), NULL, 0, NULL);
system("pause");
return 0;
}
//DoMusic.cpp
#include "stdafx.h"
#include
#include
#pragma comment(lib, "winmm.lib")
int main()
{
mciSendString(TEXT("close .\\World.mp3 repeat"), NULL, 0, NULL);
system("pause");
return 0;
}