简体   繁体   中英

How to add sound in c++ visual studio

Actually i am developing a Alarm Clock and i want to play sound (mp3 or ogg or wav file from my computer when alarm rings.

class alarmclock //Alarm Clock Declaration
{
 protected:
int hour, min, sec, A_hour, A_min, A_sec, msec;
public:
char c, a;

void time(int ah, int am, int as)
{
    SYSTEMTIME time;
    GetLocalTime(&time); //Getting Time from Local Machine


        if (ah >= 0 && ah <= 23 && am >= 0 && am <= 60 && as >= 0 && as <= 60)
        {
            hour = time.wHour;
            min = time.wMinute;
            sec = time.wSecond;
            A_hour = ah;
            A_min = am;
            A_sec = as;
            system("cls"); //Clear Screen
            for (int i = sec; sec <= 60; i++)
            {
                Sleep(1000); //Delay of 1 Sec
                system("cls");
                logo();
                gotoxy(45,13);
                cout << "=> Alarm Time: " << A_hour << ":" << A_min << ":" << A_sec<<endl;
                gotoxy(45,14);
                cout << "=> Current Time: " << hour << ":" << min << ":" << sec;
                sec++;
                if (sec == 60)
                {
                    min++;
                    if (min == 60)
                    {
                        min = 0;
                        hour++;
                    }
                    sec = 0;
                }
                if (hour == 24)
                {
                    hour = 0;
                }
                if (A_hour == hour&&A_min == min&&A_sec == sec)
                {
                    cout << "\nAlarm!";
                    printf("\a"); //with this code it only beeps once  but i want to play sound file for specific time 
                    break;
                }

                if (_kbhit())
                {
                    c = _getch();
                    cout << endl << endl;
                    if (c == 'e')
                    {
                        break;
                    }
                }
            }
        }

    else
    {
        cout << "Not Possible" << endl;
    }
}
};

I want to play a specific file for some time when alarm rings that's it. How i would be able to do that is there any lib/header file or builtin function to do that or i have to develop it on my own?

最简单的方法是使用PlaySound函数(确保与winmm.lib链接)。

The following example plays a sound file:
PlaySound(TEXT("recycle.wav"), NULL, SND_FILENAME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM