请帮我用c++写一个音乐播放器
以下是一个简单的C++音乐播放器的示例代码:
#include <iostream>
#include <Windows.h>
void playMusic(const std::string& filePath) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wideFilePath = converter.from_bytes(filePath);
PlaySoundW(wideFilePath.c_str(), NULL, SND_FILENAME | SND_ASYNC);
}
void stopMusic() {
PlaySound(NULL, NULL, SND_ASYNC);
}
int main() {
std::string filePath;
std::cout << "请输入音乐文件路径:";
std::cin >> filePath;
playMusic(filePath);
std::cout << "正在播放音乐..." << std::endl;
std::cout << "按任意键停止播放" << std::endl;
std::cin.ignore();
std::cin.get();
stopMusic();
return 0;
}
请注意,这只是一个简单的示例代码,仅支持播放WAV格式的音乐文件。如果要实现更复杂的功能,如播放其他格式的音乐文件、暂停、调整音量等,你可能需要使用额外的库来辅助实现
原文地址: https://www.cveoy.top/t/topic/h5R2 著作权归作者所有。请勿转载和采集!