首先應當去引用 相應的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;
}
測試代碼就寫到這裏。