C++ 在多线程中使用mciSendString播放音乐 上篇

假设你正在学习游戏编程,随着学习的深入,终于要开始学习音乐播放了,能让自己编写的游戏播放音乐,那是件多么美好的事情啊!

于是乎,你根据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
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值