2020年5月12日 星期二

week11

今天要開始把聲音加入我們的檔案中,首先我們要include不同的函式庫進去分別為,<windows.h>、 <mmsystem.h>這兩個函式。
#include <stdio.h>
#include <windows.h> //!!
#include <mmsystem.h> //注意兩個不能反了
#include <GL/glut.h> //播放MP3的函式
#include "CMP3_MCI.h"
CMP3_MCI 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("bongos.wav",NULL,SND_SYNC); \\當我們按下去撥放wav
}
int main(int argc,char**argv)
{
    myMP3.Loat(".mp3");//撥放檔案
    myMP3.Play();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("sound");

    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutMainLoop();
}
如果我們要播很多歌呢?我們只要加入鍵盤的判定讓他觸發呢?
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3,song1;
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("Usa.wav",NULL,SND_SYNC);
}
void key(unsigned char key,int x,int y) //鍵盤的部分
{
    switch(key)
    {
        case 'q':
            myMP3.Play();
            song1.Stop();
            break;
        case 'w':
            song1.Play();
            myMP3.Stop();
            break;
        case 'p':
            song1.Stop();
            myMP3.Stop();
            break;
    }
}

int main(int argc,char**argv)
{
    myMP3.Load("123.mp3");
    song1.Load("456.mp3");
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("sound");
    glutKeyboardFunc(key);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言