我正在嘗試從 win32 C 控制臺應用程式播放 mp3 檔案。從我在線閱讀的內容來看,mciSendString是我正在尋找的 API,我希望以下內容可以作業,但沒有。
#include <cstdio>
#include <array>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
using namespace std;
#pragma comment(lib, "Winmm.lib")
int main() {
std::array<char, MAXERRORLENGTH> errorString;
mciGetErrorStringA(
mciSendStringA(
"open \"C:\\path\\to\\1_.mp3\" type mpegvideo alias click1",
nullptr,
0,
nullptr),
errorString.data(),
MAXERRORLENGTH);
std::printf("%s\n", errorString.data());
mciGetErrorStringA(
mciSendStringA("play click1", nullptr, 0, nullptr),
errorString.data(),
MAXERRORLENGTH);
std::printf("%s\n", errorString.data());
}
我在 Visual Studio 2019 中創建了一個新的 C 控制臺應用程式,構建了這段代碼,然后從控制臺運行它。它列印:
The specified command was carried out.
The specified command was carried out.
在我的耳機中,我聽到了短暫的砰砰聲,但僅此而已。會發生什么?我是否首先需要配置音頻設備?設定音量?
uj5u.com熱心網友回復:
呼叫 mci 命令后,程式將結束。當行程退出時,音樂將停止播放。
只需在程式末尾添加一些內容即可防止其退出。
取而代之的是:
std::printf("%s\n", errorString.data());
}
這個:
std::printf("%s\n", errorString.data());
printf("press ctrl-c to exit");
while (1) {
Sleep(1000);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314725.html
