2020年5月12日 星期二

week11

播放wav音檔



將wav音檔放入freeglut裡的bin資料夾裡面

程式碼:
  ///(1)GLUT 專案,把freeglut裝好
  ///(2)把GLUT專案的程式,全刪,換下面

#include <stdio.h>
#include <windows.h>   ///為了一些定義DWORD
#include <mmsystem.h>   ///為了 PlaySoundA()
int main()
{     ///只能用 wav檔,不可mp3檔
    PlaySoundA("bong.wav",NULL, SND_SYNC); ///這裡放檔名
    printf("Hello World\n");
}

按畫面撥放音檔

#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 button,int state,int x,int y)
{  ///TODO2: mouse要發出聲音喔!
    if(state==GLUT_DOWN) PlaySoundA("bong.wav",NULL, SND_SYNC);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week12 sound");
    glutMouseFunc(mouse);   ///TODO2: mouse要發出聲音喔!
    glutDisplayFunc(display);
    glutMainLoop();
}
撥放mp3檔


將播放mp3所需檔案放入專案資料夾裡


#include <GL/glut.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h" ///TODO3: 雙引號,表示要在同一個專案目錄找
CMP3_MCI myMP3; ///TODO3: 宣告一個物件變數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("bong.wav",NULL, SND_SYNC);
}
int main(int argc,char**argv)
{
    myMP3.Load("123.mp3"); ///TODO3:讀入檔案,小心! 執行目錄(in...)
    myMP3.Play();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week12 sound");
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutMainLoop();
}


if(state==GLUT_DOWN) PlaySoundA("bong.wav",NULL, SND_ASYNC);  ///不等他,不同步
if(state==GLUT_DOWN) PlaySoundA("bong.wav",NULL, SND_SYNC);    ///等他播完,同步 



 GLUT .cbp 專案檔的設定

用Notepad++ 將圖片中的兩個路徑改為小數點 " . "
按儲存

回到CodeBlocks選Yes

將剛剛放在freeglut / bin資料夾內的音效 移至專案資料裡
這樣就完成了


Do Re Mi 聲音
將聲音檔放在專案資料夾裡
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("so.wav",NULL,SND_ASYNC);
}

讓聲音可以同時撥放


CMP3_MCI Do,Re,Mi,Fa,So;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') Do.Play();
    if(key=='2') Re.Play();
    if(key=='3') Mi.Play();
    if(key=='4') Fa.Play();
    if(key=='5') So.Play();
}




沒有留言:

張貼留言