下載音檔 . wav
將音檔放入專案中
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
PlaySoundA("Do.wav", NULL, SND_SYNC);
PlaySoundA("Re.wav", NULL, SND_SYNC);
PlaySoundA("Mi.wav", NULL, SND_SYNC);
return 0;
}
2. 利用freeglut 的 keyboard彈出音效
Build options -> Linker settings -> Link libraries -> add "freeglut", "opengl32", "glu32","gdi32", "winmm"
Build options -> Search directories -> Compiler : freeglut\include
|-> Linker : freeglut\lib
#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_SYNC);
if(key=='2')PlaySoundA("Re.wav", NULL, SND_SYNC);
if(key=='3')PlaySoundA("Mi.wav", NULL, SND_SYNC);
if(key=='4')PlaySoundA("Fa.wav", NULL, SND_SYNC);
if(key=='5')PlaySoundA("Sol.wav", NULL, SND_SYNC);
if(key=='6')PlaySoundA("La.wav", NULL, SND_SYNC);
if(key=='7')PlaySoundA("Si.wav", NULL, SND_SYNC);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D and Sound");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
CMP3_MCI player;
float potx=-1,poty=0;
void display()
{
glClearColor(0.5,0.5,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potx,poty,0);
glutSolidTeapot(0.1);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1')PlaySoundA("Do.wav", NULL, SND_SYNC);
if(key=='2')PlaySoundA("Re.wav", NULL, SND_SYNC);
if(key=='3')PlaySoundA("Mi.wav", NULL, SND_SYNC);
if(key=='4')PlaySoundA("Fa.wav", NULL, SND_SYNC);
if(key=='5')PlaySoundA("Sol.wav", NULL, SND_SYNC);
if(key=='6')PlaySoundA("La.wav", NULL, SND_SYNC);
if(key=='7')PlaySoundA("Si.wav", NULL, SND_SYNC);
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
PlaySoundA("Si.wav",NULL,SND_ASYNC);
}
}
void timer(int t)
{
glutTimerFunc(30,timer,t+1);
potx+=0.03;
if(potx>1)potx=-1;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D and Sound");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(30,timer,0);
glutMouseFunc(mouse);
player.Load("youkai.mp3");
player.Play();
glutMainLoop();
return 0;
}





沒有留言:
張貼留言