【内部资料】c++定时播放音乐
作者:野牛程序员:2023-12-27 13:21:24 C++阅读 2332
#include <iostream> #include <cstdlib> #include <ctime> #include <thread> #include <chrono> #include <Windows.h> void playMusic() { // Replace "your_music_file.mp3" with the actual path to your MP3 file const char* musicFilePath = "E:\\\\music\\\\zy.mp3"; // Use Windows API to open the default media player and play the MP3 file ShellExecuteA(NULL, "open", musicFilePath, NULL, NULL, SW_SHOWNORMAL); //ShellExecuteA(NULL, "open", commandLine, musicFilePath, NULL, SW_HIDE); // Sleep to allow time for the music to play (adjust as needed) std::this_thread::sleep_for(std::chrono::seconds(10)); // Use Windows API to close the media player // Note: This may not work for all players, and the behavior may vary ShellExecuteA(NULL, "close", "wmplayer.exe", NULL, NULL, SW_SHOW); } int main() { // Set the seed for random number generation std::srand(std::time(0)); // Define the time range in seconds for playing music const int minTime = 5; // Minimum time in seconds const int maxTime = 185; // Maximum time in seconds while (true) { // Generate a random time within the specified range int randomTime = std::rand() % (maxTime - minTime + 1) + minTime; // Convert the random time to milliseconds auto timeToSleep = std::chrono::seconds(randomTime); // Sleep for the random time interval std::this_thread::sleep_for(timeToSleep); // Play the music playMusic(); } return 0; }
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
- 上一篇:C++memset() 数组初始化
- 下一篇:c++虚函数指针