CC++播放音乐的函数的学习

CC++播放音乐的函数的学习,第1张

mciSendString函数
#include 
#include   //Unicode字符串需要用到
#include  //_getch获取用户输入需要用到
#pragma comment (lib,"winmm.lib")//播放音乐函数需要用到

MCIERROR mciSendString(
   LPCTSTR lpszCommand,//命令字符串
   LPTSTR  lpszReturnString,//指向接收返回信息的缓冲区,为NULL时不返回信息
   UINT    cchReturn,//上述缓冲区大小
   HANDLE  hwndCallback//在命令串中含通知时,它指定一个回调窗口的句柄,一般为NULL
);

后面三个参数似乎暂时不需要,就全部默认填上行了。

第一个参数

常用命令

1.open: 

_stprintf_s(
  lpszCommand, 
  TEXT("open %s %s %s"), 
  lpszDevice, //设备名称,就是媒体名字
  lpszOpenFlags, 
  lpszFlags
); 

eg:mciSendString(L"open music.wav alias BGM", NULL, 0, NULL)

music.wav : 媒体名字

alias BGM: 起别名为"BGM"

2.play

这里举几个例子 "play BGM form 3000 to 5000"

BGM:   别名

form 3000  :从第3秒开始播放(3*1000毫秒)

to 5000:到第5秒结束。

play的参数表
ValueMeaning
at timeIndicates when the device should begin performing this command, or, if the device has been cued, when the cued command begins. For more information, see the cue command.
fastIndicates that the device should play faster than normal. To determine the exact speed on a videodisc player, use the "speed" flag of the status command. To specify the speed more precisely, use the "speed" flag of this command.
from position Specifies a starting position for the playback. If the "from" flag is not specified, playback begins at the current position. For cdaudio devices, if the "from" position is greater than the end position of the disc, or if the "from" position is greater than the "to" position, the driver returns an error. For videodisc devices, the default positions are in frames for CAV discs and in hours, minutes, and seconds for CLV discs.
fullscreenSpecifies that a full-screen display should be used. Use this flag only when playing compressed files. (Uncompressed files won't play full-screen.)
repeatSpecifies that playback should restart when the end of the content is reached.
reverseSpecifies that the play direction is backward. You cannot specify an ending location with the "reverse" flag. For videodiscs, "scan" applies only to CAV format.
scanPlays as fast as possible without disabling video (although audio might be disabled). For videodiscs, "scan" applies only to CAV format.
slowPlays slowly. To determine the exact speed on a videodisc player, use the "speed" flag of the status command. To specify the speed more precisely, use the "speed" flag of this command. For videodiscs, "slow" applies only to CAV format.
speed integerPlays a videodisc at the specified speed, in frames per second. This flag applies only to CAV discs.
to positionSpecifies an ending position for the playback. If the "to" flag is not specified, playback stops at the end of the content. For cdaudio devices, if the "to" position is greater than the end position of the disc, the driver returns an error. For videodisc devices, the default positions are in frames for CAV discs and in hours, minutes, and seconds for CLV discs.
windowSpecifies that playing should use the window associated with the device instance. This is the default setting.

下面是两个使用例

void play() {
	//打开music.wav文件起个新名字BGM
	mciSendString(L"open music.wav alias BGM", NULL, 0, NULL);
	mciSendString(L"play BGM", NULL, 0, NULL);
	while (1);//防止马上停止
}
void play2() {
	//但也可以直接播放,不取名字
	mciSendString(L"play music.wav", NULL, 0, NULL);
	while (1);//防止马上停止
}

其他常用指令

1)停止(pause ):"pause music"

2)继续(Resume):“resume music”
注意:暂停后发出Play命令也可以继续

3)停止:"stop music"

4)关闭:"close music"

5)快进/后退一个位置:"step music by frames reverse"  下划线为参数

这个好像是视频用的,音频用不了

6)获取进度:"status music position"

7)获取长度:"status music length"        同上都是status命令,但是参数不同

结合鼠标消息

#define PLAY 1
#define STOP 0

void play() {
	mciSendString(L"open music.wav alias BGM", NULL, 0, NULL);
	while (1) {
		input=mouse();
		if (input == PLAY)
			mciSendString(L"play BGM from 9000 ", NULL, 0, NULL);
		if(input==STOP)
			mciSendString(L"pauseBGM", NULL, 0, NULL);
	}
}

void Button() {
	TCHAR str[] = TEXT("播放");
	settextstyle(60, 35, str);
	outtextxy(500, 250, str);
	TCHAR str2[] = TEXT("暂停");
	settextstyle(60, 35, str2);
	outtextxy(500, 450, str2);
}

int isButton(ExMessage mouse) {
	if (mouse.x > 500 && mouse.x < 500 + 60 && mouse.y>250 && mouse.y < 250 + 35)
		return PLAY;
	if (mouse.x > 500 && mouse.x < 500 + 60 && mouse.y>450 && mouse.y < 450 + 35)
		return STOP;
	return -1;
}

int mouse() {
	ExMessage mouse;
	while (1) {
		mouse = getmessage(EM_MOUSE);
		if (mouse.message == WM_LBUTTONDOWN) {
			return isButton(mouse);
		}

	}
}

void ini() {
	initgraph(1200, 600);
	Button();
	play3();
}

效果

 可以播放音乐,也可以暂停,至于按钮的位置只进行了粗略的计算,学习函数使用为主。

参考博客  C/C++笔记之播放音乐的函数_搬砖的码蚁的博客-CSDN博客_music函数

参考文档MCI Command Strings - Win32 apps | Microsoft Docs

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/756563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-04-30

发表评论

登录后才能评论

评论列表(0条)

保存