我的简易闹钟

#include <iostream>
#include <chrono>
#include <ctime>
#include <thread>
#include <cstdlib> // For system("clear") on Linux/Mac
#include <string>  // For std::string
#include "../audio_clip/audio_clip.cpp"

#ifdef _WIN32
#include <windows.h>
#include <mmsystem.h>
#else
#include <unistd.h> // For usleep() on Linux/Mac
#endif

void clearConsole() {
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}

int main(int argc, char* argv[]) {
    int h = 13, m = 0, s = 0;
    bool alarmRang = false;

    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <audio_file_path>" << std::endl;
        return 1;
    }
    std::string audioFilePath = argv[1];

    while (!alarmRang) {
        clearConsole();

        auto now = std::chrono::system_clock::now();
        std::time_t now_c = std::chrono::system_clock::to_time_t(now);
        std::tm localNow;
#ifdef _WIN32
        localtime_s(&localNow, &now_c);
#else
        localtime_r(&now_c, &localNow);
#endif

        std::cout << localNow.tm_hour << "时"
            << localNow.tm_min << "分"
            << localNow.tm_sec << "秒" << std::endl
            << "时间截至于:" << h << "时" << m << "分" << s << "秒" << std::endl;

        if (localNow.tm_hour == h && localNow.tm_min == m && localNow.tm_sec == s) {
            AudioClip ac;
            ac.load(audioFilePath);
            ac.play();
            alarmRang = true;
        }

        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    return 0;
}
源文件

#include <string><pre name = "code" class = "cpp">
#pragma once
#include<iostream>
#include<Windows.h>
#include <wtypes.h>
typedef int(__stdcall* w32mci)(const char*, char*, int, int);
typedef int(__stdcall* w32mcierror)(int, char*, int);

class Mci {
private:
    HINSTANCE hins;
    w32mci wmci;
    w32mcierror wmcierror;

public:
    Mci();  // 构造函数声明
    ~Mci(); // 析构函数声明
    char buf[256];
    bool send(std::string command); // 函数声明
};
class AudioClip
{
private:
    Mci mci;
    std::string filename;
    std::string alias;
    int length_ms;
public:
    AudioClip();
    ~AudioClip();
    bool load(const  std::string& _filename);
    bool play(int start_ms = 0, int end_ms = -1);
    bool stop();
    bool pause();
    bool unpause();
    int milliseconds();
};

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值