假设你正在学习游戏编程,随着学习的深入,终于要开始学习音乐播放了,能让自己编写的游戏播放音乐,那是件多么美好的事情啊!
于是乎,你根据MCI的相关资料,简单编写了一个音乐播放程序,代码如下:
Eg1:
#include <Windows.h>
#include <string>
#include <iostream>
#pragma comment(lib,"winmm.lib")
//发送信息给MCI
bool SendToMCI(std::string command)
{
if(!mciSendString(command.c_str(),NULL,0,0))
return true;
return false;
}
//从MCI获取信息
std::string GetFromMCI(std::string command)
{
char message[20];
mciSendString(command.c_str(),message,20,0);
std::string str(message);
return str;
}
//入口函数
int main(int argc, char *argv[])
{
std::string filepath="歌唱祖国.mid";
std::string Open="OPEN "+filepath+" ALIAS MUSIC";
std::string Status="status MUSIC mode";
std::string Play="PLAY MUSIC FROM 0";
std::string Close="CLOSE MUSIC";
SendToMCI(Open); //打开音乐文件
long Tick=0;
std::string result="";
while(true)
{
Sleep(1);
Tick=timeGetTime();
result=GetFromMCI(Status); //获取音乐状态
if(result=="stopped")
SendToMCI(Play); //如果音乐停止,重新播放
if(timeGetTime()-Tick>100)
std::cout<<"卡了一下!\n";
}
SendToMCI(Close); //关闭音乐文件
retur