今日主題:加入音樂檔
開啟codeblock新增一個project

到網路上下載一個wav音樂檔

將音樂檔路逕貼到PlaySound裡面

注意!反斜線加英文會有特殊用意所以要再加一條反斜線

此時執行檔案還是會有錯誤!!!
解決方法:加入連結
在專案按右鍵build option
在link加入加入winmm

執行程式碼後就能播出音檔的聲音了
程式碼:
#include <windows.h>///(0)為了mmsystem.h裡面有些宣告,需要在之前有windows.h #include<mmsystem.h>///(1)MultiMedia System可播聲音 int main() { PlaySoundA("C:\\Users\\user\\Downloads\\thunder_clap.wav",NULL,SND_SYNC); ///(2)播放聲音ascii版Wav檔thunder_clap.wav ///空 ///聲音等待同步 return 0; }
2.邊播聲音邊開下個視窗
(1)複以前的步驟:搜尋freeglut->下載->解壓縮拖移到桌面->複製、改檔名(libglut32.a)->開啟codeblock新建一個新的GLUT project
(2)在程式碼裡加入下面幾行

#include <stdio.h> #include<mmsystem.h>///(1)MultiMedia System可播聲音 int main(int argc, char *argv[]) { PlaySoundA("C:\\Users\\user\\Downloads\\thunder_clap.wav",NULL,SND_ASYNC); ///(2)播放聲音ascii版Wav檔thunder_clap.wav ///空 ///SND_ASYNC聲音等待不同步 printf("邊播聲音繼續開下面視窗\n");
(3)做出簡單鋼琴:用鍵盤1245控制發出do re mi fa so

#include<mmsystem.h>///now2:現在要在這個keyboard函式裡做出簡單的鋼琴 static void key(unsigned char key, int x, int y) { if(key=='1')PlaySoundA("C:\\Users\\user\\Downloads\\do.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴 if(key=='2')PlaySoundA("C:\\Users\\user\\Downloads\\re.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴 if(key=='3')PlaySoundA("C:\\Users\\user\\Downloads\\mi.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴 if(key=='4')PlaySoundA("C:\\Users\\user\\Downloads\\fa.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴 if(key=='5')PlaySoundA("C:\\Users\\user\\Downloads\\so.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
(4)要將這個檔案下載下來丟進專案檔裡面

執行後就會先播出打雷聲接著按12345就能控制do re mi fa so
(5)讀入mp3檔

#include "CMP3_MCI.h"///(1)這個外掛不是系統有的,而是放在我們程式專案目錄裡面 CMP3_MCI myMP3;///(2)宣告一個物件變數 myMP3;

myMP3.Load("C:\\Users\\user\\Downloads\\music.mp3");///now3(3)Load讀入mp3檔 myMP3.Play();///now3(4)play播放 ///PlaySoundA("C:\\Users\\user\\Downloads\\thunder_clap.wav",NULL,SND_ASYNC); ///(2)播放聲音ascii版Wav檔thunder_clap.wav ///空 ///SND_ASYNC聲音等待不同步
執行出來後可開啟視窗播放mp3檔
--------------------------------------------------------------------------------------------------------------------------
全部程式碼:
myMP3.Load("C:\\Users\\user\\Downloads\\music.mp3");///now3(3)Load讀入mp3檔
myMP3.Play();///now3(4)play播放
#include "CMP3_MCI.h"///(1)這個外掛不是系統有的,而是放在我們程式專案目錄裡面
CMP3_MCI myMP3;///(2)宣告一個物件變數 myMP3;
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
static int slices = 16;
static int stacks = 16;
/* GLUT callback Handlers */
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidTorus(0.2,0.8,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(-2.4,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireTorus(0.2,0.8,slices,stacks);
glPopMatrix();
glutSwapBuffers();
}
#include<mmsystem.h>///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
static void key(unsigned char key, int x, int y)
{
if(key=='1')PlaySoundA("C:\\Users\\user\\Downloads\\do.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
if(key=='2')PlaySoundA("C:\\Users\\user\\Downloads\\re.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
if(key=='3')PlaySoundA("C:\\Users\\user\\Downloads\\mi.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
if(key=='4')PlaySoundA("C:\\Users\\user\\Downloads\\fa.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
if(key=='5')PlaySoundA("C:\\Users\\user\\Downloads\\so.wav",NULL,SND_ASYNC);
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
case '+':
slices++;
stacks++;
break;
case '-':
if (slices>3 && stacks>3)
{
slices--;
stacks--;
}
break;
}
glutPostRedisplay();
}
static void idle(void)
{
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
#include <stdio.h>
#include<mmsystem.h>///(1)MultiMedia System可播聲音
int main(int argc, char *argv[])
{
myMP3.Load("C:\\Users\\user\\Downloads\\music.mp3");///now3(3)Load讀入mp3檔
myMP3.Play();///now3(4)play播放
///PlaySoundA("C:\\Users\\user\\Downloads\\thunder_clap.wav",NULL,SND_ASYNC);
///(2)播放聲音ascii版Wav檔thunder_clap.wav
///空
///SND_ASYNC聲音等待不同步
printf("邊播聲音繼續開下面視窗\n");
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);///now2:現在要在這個keyboard函式裡做出簡單的鋼琴
glutIdleFunc(idle);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
沒有留言:
張貼留言