播放音檔(WAV)
程式碼:
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
int main()
{
PlaySoundA("glass.wav",NULL,SND_SYNC);\\\SND_SYNC要等到音檔撥放完才會撥下一次
\\\如果要立刻撥放可改成SND_ASYNC
printf("Hello World!");
}
加入滑鼠點擊播放音效
程式碼:
#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int botton, int state, int x, int y)
{
if(state==GLUT_DOWN)PlaySoundA("glass.wav",NULL,SND_SYNC);
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week 12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
利用CMP_MCI.h播放MP3
程式碼:
#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int botton, int state, int x, int y)
{
if(state==GLUT_DOWN)PlaySoundA("glass.wav",NULL,SND_SYNC);
}
int main(int argc,char**argv)
{
myMP3.Load("bgm_01.mp3");
myMP3.Play();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week 12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
專案檔
將兩行後面的位置都改成"."
就可以將音檔和需要用到的附加檔案加入專案檔的資料夾中就可以開啟需要用到的檔案了
鍵盤鋼琴
程式碼:
#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
CMP3_MCI Do, Re, Mi, Fa, So;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int botton, int state, int x, int y)
{
if(state==GLUT_DOWN)PlaySoundA("boom.wav",NULL,SND_ASYNC);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1')Do.Play();//PlaySoundA("do.wav",NULL,SND_ASYNC);
if(key=='2')Re.Play();//PlaySoundA("re.wav",NULL,SND_ASYNC);
if(key=='3')Mi.Play();//PlaySoundA("mi.wav",NULL,SND_ASYNC);
if(key=='4')Fa.Play();//PlaySoundA("fa.wav",NULL,SND_ASYNC);
if(key=='5')So.Play();//PlaySoundA("so.wav",NULL,SND_ASYNC);
}
int main(int argc,char**argv)
{
myMP3.Load("bgm_01.mp3");
myMP3.Play();
Do.Load("do.wav");Re.Load("re.wav");Mi.Load("mi.wav");Fa.Load("fa.wav");So.Load("so.wav");
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week 12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}







沒有留言:
張貼留言