C language to play music in the game

Play music using mciSendString

mciSendString supports multiple media formats such as mp3, wma, wav, mid, etc. It is very easy to use. Here is a simple example, using the mciSendString function to play music in MP3 format, the code is as follows:

// 编译该范例前,请把 music.mp3 放在项目文件夹中
// 发布时,请把 music.mp3 和编译的 exe 放在一起
// 编译环境:VC6~VC2019 + EasyX_20210115
//
#include <graphics.h>
#include <conio.h>
// 引用 Windows Multimedia API
#pragma comment(lib, "Winmm.lib")

int main()
{
	initgraph(640, 480);

	// 打开音乐
	mciSendString(_T("open music.mp3 alias mymusic"), NULL, 0, NULL);

	outtextxy(0, 0, _T("按任意键开始播放"));
	_getch();

	// 播放音乐
	mciSendString(_T("play mymusic"), NULL, 0, NULL);

	outtextxy(0, 0, _T("按任意键停止播放"));
	_getch();

	// 停止播放并关闭音乐
	mciSendString(_T("stop mymusic"), NULL, 0, NULL);
	mciSendString(_T("close mymusic"), NULL, 0, NULL);

	outtextxy(0, 0, _T("按任意键退出程序"));
	_getch();
	closegraph();
	return 0;
}

Briefly explain:

Be sure to reference the Winmm.lib library file. In this example, it is referenced by the #pragma comment command, and it can also be set in the project properties, which will not be introduced here.

The function of the mciSendString function is very powerful, and it can even play videos, but there is no introduction here, please refer to MSDN for details. In most cases, only the first parameter is needed, and the other three parameters can be set to NULL, 0, NULL.

The first parameter is a multimedia command string, case insensitive. In the program, first open the background.mp3 through the open command, and use the alias to specify the alias "mymusic", so that the music can be easily accessed through the alias "mymusic" in the subsequent code. Of course, it is not necessary to specify an alias, and it is also possible to access by file name every time.

The mp3 after open can use absolute path or relative path.

Then it is: play mymusic starts playing, stop mymusic stops playing, and close mymusic closes the file.

If you need to play multiple music at the same time, please specify different aliases for different music, and then operate them separately.

Please remember to use the close command to close the music that is no longer needed.

There is also a PlaySound function that can also be used to play sound, but unfortunately it does not support mp3 / wma, so I won't introduce it here.

Some useful multimedia commands:

Play xxx from the beginning:

"play xxx from 0"

Play a .mp3 file in a loop (.wma files also work):

"open xxx.mp3"
"play xxx.mp3 repeat"

Play a .wav file in a loop (.mid files also work):

"open xxx.wav type MPEGVideo"
"play xxx.wav repeat"

Close all multimedia files opened by the current program:

"close all"

If there are spaces in the file name, you need to enclose the file name in double quotes (note the escape):

mciSendString("open \"D:\\My Music\\俞丽拿 梁祝 化蝶.mp3\" alias mymusic", NULL, 0, NULL);

Play music with mciSendCommand

The functions of mciSendCommand and mciSendString are similar, but the control method is slightly different. Therefore, without too much introduction to mciSendCommand, just look at a usage example. The following example realizes the loop playback of mp3:

// 该范例演示使用 mciSendCommand 函数循环播放 mp3
// 编译环境:VC2008~VC2019 + EasyX_20210115
//
#include <graphics.h>
#include <Digitalv.h>
#include <conio.h>
#pragma comment(lib, "winmm.lib")

int main()
{
	initgraph(640, 480);

	// 打开音乐文件
	MCI_OPEN_PARMS mciOpenParms;
	mciOpenParms.lpstrDeviceType = L"sequencer";
	mciOpenParms.lpstrElementName = L"R:\\Downloads\\test.mp3";
	mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_ELEMENT | MCI_WAIT, DWORD_PTR(&mciOpenParms));	// 打开指定媒体文件
	UINT wDeviceID = mciOpenParms.wDeviceID;	// 返回的 Device ID 需要保存下来,以便实现后续控制

	outtextxy(0, 0, L"按任意键开始播放");
	_getch();

	// 播放音乐
	MCI_PLAY_PARMS mciPlayParms;
	mciSendCommand(wDeviceID, MCI_PLAY, MCI_DGV_PLAY_REPEAT, DWORD_PTR(&mciPlayParms));

	outtextxy(0, 0, L"按任意键停止播放");
	_getch();

	// 停止播放并关闭音乐
	mciSendCommand(wDeviceID, MCI_STOP, 0, NULL);	// 停止播放
	mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);	// 关闭音乐

	outtextxy(0, 0, L"按任意键退出程序");
	_getch();
	closegraph();
	return 0;
}

Solve the problem that some MP3 cannot be played

Some mp3s cannot be played by mciSendString because they use a relatively large cover. I have tested an mp3 whose cover size is 1824x1824 and cannot be played directly. Change the cover size to 500x500 and play it smoothly. Of course, the easier way is to remove the cover directly. Here I recommend a tool for everyone: Mp3tag, official website: https://www.mp3tag.de , the operation method of Mp3tag is very simple, just click the mouse to get it done, so I won’t go into details here.

Use the PlaySound function to play music

PlaySound can be used to play wav and system sound. It is very simple to use. Some common usages are as follows:

// 注意:
// 1. 需要使用预编译指令 #pragma comment(lib, "winmm.lib") 链入库文件
// 2. 第一个参数是字符串,注意使用程序中约定的字符集,例如 Unicode 字符集需要在字符串前面加 L
//
PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_SYNC);				// 播放 Bells.wav,并等待直到播放结束
PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_ASYNC);			// 后台播放 Bells.wav,函数立即返回
PlaySound("SystemExclamation", NULL, SND_ASYNC);				// 后台播放系统定义的 SystemExclamation 声音
PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_LOOP | SND_ASYNC);	// 后台循环播放 Bells.wav
PlaySound(NULL, NULL, 0);										// 停止后台播放

For more interesting small projects, see my 哔哩哔哩, Q skirt: Xiaoyu Come Come My Personal Space-Little Fish Come Come My Personal Homepage-Bilibili Video Bilibili Xiaoyu Come Come My Personal Space , providing video, audio, articles, dynamics, favorites and other content shared by Xiaoyu Come, follow Xiaoyu Come Come account, and learn about UP's dynamics as soon as possible. Programming learning group: 725022484 Share a small programming game every day~C/C++ game source code materials and various installation packages, private messages are not often seen! https://space.bilibili.com/1827181878?spm_id_from=333.1007.0.0

Guess you like

Origin blog.csdn.net/yx5666/article/details/129016532