C/C++音樂播放(親測有效)

首先應當去引用 相應的lib庫
下載地址:http://www.opdown.com/soft/125224.html
ps:lib庫的位置最好不要放到帶有中文路徑的文件夾下

#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")

int main()
{
	//abc.mp3是一個包含了路徑的字符串,我這裏放到了工程文件下
	FILE* pf = nullptr;
	fopen_s(&pf, "abc.mp3", "rb");
	if (pf == nullptr)
		std::cout << "文件不存在\n";

	mciSendString(TEXT("open abc.mp3 alias MySong"), NULL, 0, NULL);
	mciSendString(TEXT("play MySong"), NULL, 0, NULL);

	//控制命令:open  play   pause  resume  close  loop
	while (true)
	{
		int input = getchar();
		//暫停
		if (input == 'a')
		{
			mciSendString(TEXT("pause MySong"), NULL, 0, NULL);
		}
		//繼續
		else if (input == 's')
		{
			mciSendString(TEXT("resume MySong"), NULL, 0, NULL);
		}
		//關閉
		else if (input == 'd')
		{
			mciSendString(TEXT("close MySong"), NULL, 0, NULL);
		}
	}
	return 0;
}

測試代碼就寫到這裏。

發佈了19 篇原創文章 · 獲贊 16 · 訪問量 1204
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章