開一個空檔案
語言選擇C++
環境參數不用更改
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
int main()
{
int a;
for(int i=0;i<10;i++)
{
cout << "Hello world!" << endl;
PlaySoundA("Do.wav",NULL,SND_ASYNC); //輸入音檔 ASYNC 為非同步,不用等
cin>>a;
}
return 0;
}
執行後輸入數字1,可以聽到Do的
鍵盤 Piano
開一個檔案
設定檔案路徑、咒語
按OK後
輸入程式碼
#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
void display()
{
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') PlaySoundA("Do.wav",NULL,SND_ASYNC);
if(key=='2') PlaySoundA("Re.wav",NULL,SND_ASYNC);
if(key=='3') PlaySoundA("Mi.wav",NULL,SND_ASYNC);
if(key=='4') PlaySoundA("Fa.wav",NULL,SND_ASYNC);
if(key=='5') PlaySoundA("Sol.wav",NULL,SND_ASYNC);
if(key=='6') PlaySoundA("La.wav",NULL,SND_ASYNC);
if(key=='7') PlaySoundA("Si.wav",NULL,SND_ASYNC);
}
int main(int argc,char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D sound version");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
結果

播放下載背景音樂
下載音樂檔放入資料夾中
打入程式
#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h" 新增
CMP3_MCI myMP3;
void display()
{
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') PlaySoundA("Do.wav",NULL,SND_ASYNC);
if(key=='2') PlaySoundA("Re.wav",NULL,SND_ASYNC);
if(key=='3') PlaySoundA("Mi.wav",NULL,SND_ASYNC);
if(key=='4') PlaySoundA("Fa.wav",NULL,SND_ASYNC);
if(key=='5') PlaySoundA("Sol.wav",NULL,SND_ASYNC);
if(key=='6') PlaySoundA("La.wav",NULL,SND_ASYNC);
if(key=='7') PlaySoundA("Si.wav",NULL,SND_ASYNC);
}
int main(int argc,char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D sound version");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myMP3.Load("cat.mp3"); 新增
myMP3.Play(); ↑音樂檔名
glutMainLoop();
}








沒有留言:
張貼留言