简体   繁体   English

如何在C ++ Visual Studio中添加声音

[英]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. 实际上,我正在开发一个闹钟,并且想在闹钟响时播放计算机上的声音(mp3或ogg或wav文件)。

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? 我怎么能做到这一点,有没有任何lib / header文件或内置函数可以做到这一点,或者我必须自己开发它?

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

The following example plays a sound file: 以下示例播放声音文件:
PlaySound(TEXT("recycle.wav"), NULL, SND_FILENAME); PlaySound(TEXT(“ recycle.wav”),NULL,SND_FILENAME);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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