PlaySound用程式碼播放聲音
一如往常打開glut檔案
之後更改程式碼
要將下載的聲音檔案 [cannon.wav]
放入 freeglut > bin 裡面
#include <stdio.h>
#include <windows.h>///為了定義DWORD
#include <mmsystem.h>///為了PlaySoundA()
int main()
{
PlaySoundA("cannon.wav",NULL,SND_SYNC);///播放音檔cannon
printf("jk\n");
}
更改成以下程式碼
就可以按下滑鼠就發出聲音
#include <stdio.h>
#include <windows.h>///為了定義DWORD 一定要放在playsound上面
#include <mmsystem.h>///為了PlaySoundA()
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN) PlaySoundA("cannon.wav",NULL,SND_SYNC);
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week12 Sound");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}
再更改以下程式碼
可以直接播放mp3檔案
#include <stdio.h>
#include <windows.h>///為了定義DWORD 一定要放在playsound上面
#include <mmsystem.h>///為了PlaySoundA()
#include <GL/glut.h>
#include "CMP3_MCI.h" ///雙引號 表示在同一個傳案目錄裡找
CMP3_MCI myMP3; ///宣告一個物件變數myMP3
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN) PlaySoundA("cannon.wav",NULL,SND_SYNC);
}
int main(int argc, char**argv)
{
myMP3.Load("jk.mp3"); ///讀入檔案
myMP3.Play();///播放
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week12 Sound");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言