❶ 如何寫一個可以播放音樂的程序(用簡單C語言編寫,可以給出音頻文件,只需要播放即可)
用VC++編譯通過,代碼如下(其中a.wav為自己給出):
#include<windows.h>
#pragma comment(lib,"winmm.lib")
void main()
{
PlaySound("C:\\a.wav",0,SND_FILENAME|SND_SYNC);
}
❷ C語言 怎麼編程播放WAV音樂
可以使用PlaySound()函數播復放聲制音文件,
該函數原型位於#include<mmsystem.h>中,
函數原型為:BOOLPlaySound(LPCSTRpszSound,HMODULEhmod,DWORDfdwSound);
參數pszSound是要播放聲音的文件名,
只支持WAV等格式的文件
參數hmod是應用程序的實例句柄,一般傳遞NULL就可以;
參數fdwSound是標志的組合掩碼,
可選值有SND_FILENAME、SND_ASYNC、SND_SYNC等。
SND_FILENAME表示pszSound參數指定的是文件名(pszSound還可以指定資源、內存音樂、系統音樂等等);
SND_ASYNC:用非同步方式播放聲音,PlaySound函數在開始播放後立即返回;
SND_SYNC:同步播放聲音,在播放完後PlaySound函數才返回;
SND_LOOP一遍遍的重復播放聲音,必須與SND_ASYNC標志一塊使用。
【2、使用舉例】
PlaySound(TEXT("C:\WINDOWS\Media\WindowsXP啟動.wav"),NULL,SND_FILENAME|SND_SYNC);
❸ 怎麼把歌曲用單片機C語言程序進行播放,急急急急~~~~~~
#include <reg51.h> //包括一個51標准內核的頭文件
#define uchar unsigned char //定義一下方便使用
#define uint unsigned int
sbit BEEP=P3^6; //喇叭輸出腳
uchar th0_f; //在中斷中裝載的T0的值高8位
uchar tl0_f; //在中斷中裝載的T0的值低8位
/***************** 延時 1ms *******************/
void delay_1ms(unsigned int t)
{
unsigned char i,j;
for(t;t>0;t--)
{
for(i=34;i>0;i--)
for(j=13;j>0;j--);
}
}
//T0的值,及輸出頻率對照表
static code freq[36][2] = {
0xA9,0xEF, //00220HZ ,1 //0
0x93,0xF0, //00233HZ ,1# //1
0x73,0xF1, //00247HZ ,2 //2
0x49,0xF2, //00262HZ ,2# //3
0x07,0xF3, //00277HZ ,3 //4
0xC8,0xF3, //00294HZ ,4 //5
0x73,0xF4, //00311HZ ,4# //6
0x1E,0xF5, //00330HZ ,5 //7
0xB6,0xF5, //00349HZ ,5# //8
0x4C,0xF6, //00370HZ ,6 //9
0xD7,0xF6, //00392HZ ,6# //10
0x5A,0xF7, //00415HZ ,7 //11
0xD8,0xF7, //00440HZ 1 //12
0x4D,0xF8, //00466HZ 1# //13
0xBD,0xF8, //00494HZ 2 //14
0x24,0xF9, //00523HZ 2# //15
0x87,0xF9, //00554HZ 3 //16
0xE4,0xF9, //00587HZ 4 //17
0x3D,0xFA, //00622HZ 4# //18
0x90,0xFA, //00659HZ 5 //19
0xDE,0xFA, //00698HZ 5# //20
0x29,0xFB, //00740HZ 6 //21
0x6F,0xFB, //00784HZ 6# //22
0xB1,0xFB, //00831HZ 7 //23
0xEF,0xFB, //00880HZ `1 //24
0x2A,0xFC, //00932HZ `1# //25
0x62,0xFC, //00988HZ `2 //26
0x95,0xFC, //01046HZ `2# //27
0xC7,0xFC, //01109HZ `3 //28
0xF6,0xFC, //01175HZ `4 //29
0x22,0xFD, //01244HZ `4# //30
0x4B,0xFD, //01318HZ `5 //31
0x73,0xFD, //01397HZ `5# //32
0x98,0xFD, //01480HZ `6 //33
0xBB,0xFD, //01568HZ `6# //34
0xDC,0xFD, //01661HZ `7 //35
};
static code music1[464] = { // 《隱形的翅膀》
36,36,36,36,36,36,36,36,36,36,36,36,8,8,12,12, //每一
16,16,16,16,16,16,19,19,16,16,16,16,14,14,12,12, //次都在徘徊
12,12,12,12,12,12,9,9,8,8,8,36,9,9,12,12, //孤單中堅強 每一
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //次就算很受傷
19,19,16,16,14,14,12,12,14,14,14,36,21,21,19,19, //也不閃淚光 我知
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //道我一直有雙
16,16,14,14,12,12,14,14,9,9,9,36,8,8,9,9, //隱形的翅膀 帶我
12,12,12,12,12,12,16,16,14,14,14,14,16,14,14,14, //飛 飛過絕
12,12,12,12,12,12,12,12,12,12,12,36,8,8,12,12, //望 不去
16,16,16,16,16,16,19,19,16,16,16,16,14,14,12,12, //想他們擁有
12,12,12,12,12,12,9,9,8,8,8,36,8,8,12,12, //美麗的太陽 我看
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //見每天的夕陽
19,19,16,16,14,14,12,12,14,14,14,36,21,21,19,19, //也會有變化 我知
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //道我一直有雙
16,16,14,14,12,12,14,14,9,9,9,36,8,8,9,9, //隱形的翅膀 帶我
12,12,12,12,12,12,14,16,14,14,14,14,16,16,12,12, //飛 給我希
12,12,12,12,12,12,12,12,12,12,12,36,16,16,19,19, //望 我終
24,24,24,24,24,36,24,24,24,24,24,24,21,21,19,19, //於看到所有
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //夢想都開花 追逐
12,12,24,24,24,24,21,21,19,19,16,16,14,14,12,12, //的年輕歌聲多嘹
14,14,14,14,14,14,14,14,14,14,14,36,16,16,19,19, //亮 我終
24,24,24,24,24,36,24,24,24,24,24,24,21,21,19,19, //於翱翔用心
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //凝望不害怕 哪裡
12,12,24,24,24,24,21,21,19,19,16,16,14,14,12,12, //會有風就飛多遠
12,12,12,12,12,12,12,12,12,12,36,36,16,16,19,19, //吧 隱形
24,24,24,24,24,36,24,24,23,23,23,23,21,21,19,19, //的翅膀讓夢
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //恆久比天長 留一
12,12,24,24,24,24,21,21,19,19,16,16,14,14,14,14, //個願望讓自己
14,14,14,14,12,12,12,12,12,12,12,12,12,12,12,36, //想像... ...
};
static code music2[336] = { // 《北京歡迎你》
16,16,19,19,16,16,14,14,16,16,14,14,16,16,16,36, //迎接另一個晨曦
16,16,14,14,9,9,12,12,16,16,14,14,14,14,14,36, //帶來全新空氣
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //氣息改變情味不變
16,16,21,21,19,19,9,9,14,14,12,12,12,12,12,36, //茶香飄滿情誼
16,16,19,19,16,16,14,14,16,16,14,14,16,16,16,36, //我家大門常打開
16,16,14,14,9,9,12,12,16,16,14,14,14,14,14,36, //開放懷抱等你
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //擁抱過就有了默契
16,16,21,21,19,19,9,9,14,14,12,12,12,12,12,36, //你會愛上這里
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //不管遠近都是客人
16,16,21,21,19,19,19,19,16,16,16,16,16,16,16,36, //請不用客氣
14,14,16,16,14,14,12,12,19,19,21,21,14,14,14,36, //相約好了在一起
9,9,16,16,14,14,14,14,12,12,12,12,12,12,12,36, //我們歡迎你
36,36,36,36,36,36,36,36,16,16,16,16,19,19,19,19, //北京
24,24,19,19,21,21,21,21,36,36,21,21,19,19,16,16, //歡迎你 為你開
16,16,19,19,19,19,19,19,19,19,19,36,16,16,19,19, //天闢地 流動
21,21,24,24,26,26,24,24,19,19,16,16,14,14,19,19, //中的魅力充滿著朝
16,16,16,16,16,16,16,16,16,16,16,36,16,16,19,19, //氣 北京
24,24,19,19,21,21,21,21,36,36,24,24,26,26,24,24, //歡迎你 在太陽
19,19,16,16,19,19,23,23,21,21,21,21,36,36,16,16, //下分享呼吸 在
14,14,16,16,19,19,28,28,26,26,26,26,26,26,23,23, //黃土地刷新成
24,24,24,24,24,24,24,24,24,24,24,24,24,24,36,36, //績
};
static code music3[444] = { // 《仙劍奇俠傳》
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,24,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,28,28,26,26,36,36,36,36,28,28,26,26,
21,21,21,21,24,24,26,26,28,28,28,28,28,28,28,28,
28,28,31,31,28,28,24,24,26,26,26,26,26,26,26,26,
21,21,21,21,24,24,28,28,26,26,26,26,24,24,24,24,
21,21,21,21,21,21,21,21,21,21,21,21,36,36,36,36,
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,28,28,26,26,36,36,36,36,28,28,26,26,
28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
16,16,16,16,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,23,23,23,23,23,23,23,23,23,23,23,23,
21,21,21,21,24,24,26,26,28,28,28,28,28,28,28,28,
31,33,31,31,28,28,24,24,26,26,26,26,26,26,26,26,
21,21,21,21,24,24,28,28,26,26,26,26,24,24,24,24,
21,21,21,21,21,21,21,21,21,21,21,21,36,36,36,36,
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
23,23,23,23,23,23,23,23,23,23,23,23,21,21,19,19,
21,21,21,21,21,21,21,21,36,36,36,36,36,36,36,36,
};
static code music4[448] = { // 《笑傲江湖》
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,21,16,16,16,16,16,16,16,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,19,16,16,16,16,36,16,16,14,
12,12,12,12,14,14,16,16,14,14,14,14,14,14,14,12,
9,9,9,12,14,14,16,16,19,19,19,21,19,19,16,14,
12,12,12,14,14,14,16,16,14,14,14,14,14,12,12,12,
9,9,9,12,14,16,16,19,21,21,21,21,21,21,21,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,19,16,16,16,16,16,16,16,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,14,16,19,19,21,19,16,16,16,16,36,16,16,14,
12,12,12,12,14,14,16,16,14,14,14,14,14,14,14,12,
9,9,9,12,14,14,16,16,19,19,19,21,19,19,16,14,
12,12,12,14,14,14,16,16,14,14,14,14,14,12,12,12,
9,9,9,12,14,16,16,19,21,21,21,21,21,21,21,36,
14,14,14,14,14,16,16,14,12,12,12,12,12,9,9,12,
14,14,14,14,14,16,16,14,12,12,9,9,36,12,9,12,
14,14,14,14,14,16,16,14,12,12,12,12,36,12,9,12,
14,14,14,14,14,19,19,16,16,16,16,16,16,16,16,36,
9,9,9,9,36,9,9,9,14,14,14,14,14,14,16,14,
12,12,12,12,36,12,12,14,9,9,9,9,9,9,7,7,
9,9,9,9,36,9,7,7,4,4,4,4,36,4,7,7,
2,2,2,2,36,2,2,2,4,4,4,4,4,4,4,36,
9,9,9,9,9,9,9,9,14,14,14,14,14,14,16,16,
12,12,12,12,12,12,14,14,9,9,9,9,12,9,
7,7,7,7,7,7,9,9,4,4,4,2,4,4,7,7,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
};
//定時中斷0,用於產生唱歌頻率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //調入預定時值
BEEP=~BEEP; //取反音樂輸出IO
P2=~P2;
}
//音階聲音自動輸出試驗
void main(void) // 主程序
{
uint i;
TMOD = 0x01; //使用定時器0的16位工作模式
TR0 = 1;
ET0 = 1;
EA = 1;
while(1)
{
for(i=0;i<336;i++) //播放音樂
{
if(music2[i] == 36) //空音
{
TR0 = 0;
delay_1ms(190);
TR0 = 1;
}
else
{
tl0_f=freq[music2[i]][0]; //置一個音符的值
th0_f=freq[music2[i]][1];
delay_1ms(190);
}
}
TR0 = 0;
delay_1ms(3000);
TR0 = 1;
}
}
❹ 如何在C語言的編程中插入音樂
一.在程序中直接播放聲音文件x0dx0a在VC++ 中的多媒體動態連接庫中提供了一組與音頻設備有關的函數。利用這些函數可以方便地播放聲音。最簡單的播放聲音方法就是直接調用VC++中提供的聲音播放函數BOOL sndPlaySound ( LPCSTR lpszSound,UINT fuSound ); 或BOOL PlaySound( LPCSTR lpszSound, HMODULE hmod, DWORD fuSound );其中參數lpszSound是需要播放聲音的.W***文件的路徑和文件名, hmod在這里為NULL,fuSound是播放聲音的標志,詳細說明請參考VC++中的幫助。 例如播放C:soundmusic.wav可以用sndPlaySound ("c:\sound\music.wav",SND_ASYNC);或PlaySound("c:\sound\music.wav",NULL, SND_ASYNC|SND_NODEFAULT );如果沒有找到music.wav文件,第一種格式將播放系統默認的聲音,第二種格式不會播放系統默認的聲音。x0dx0ax0dx0a二.將聲音文件加入到程序中x0dx0a在VC++的程序設計中,可以利用各種標準的資源,如點陣圖,菜單,對話框等。同時VC++也允許用戶自定義資源,因此我們可以將聲音文件作為用戶自定義資源加入程序資源文件中,經過編譯連接生成EXE文件,實現無.W***文件的聲音播放。要實現作為資源的聲音文件的播放,首先要在資源管理器中加入待播放的聲音文件。x0dx0a具體步驟入下:x0dx0a1.獲得包含資源的模塊句柄:x0dx0aHMODULE hmod=AfxGetResourceHandle();x0dx0a2.檢索資源塊信息:x0dx0aHRSRC hSndResource=FindResource(hmod,MAKEINTRESOURCE(IDR_W***E1),_T("W***E"));x0dx0a3. 裝載資源數據並加鎖:x0dx0aHGLOBAL hGlobalMem=LoadResource(hmod,hSndResource);x0dx0aLPCTSTR lpMemSound=(LPCSTR)LockResource(hGlobalMem);x0dx0a4.播放聲音文件:x0dx0asndPlaySound(lpMemSound,SND_MEMORY));x0dx0a5.釋放資源句柄:x0dx0aFreeResource(hGlobalMem);
❺ C語言程序裡面怎麼循環播放聲音
編譯器 MS VC++ 6.0
已有 音頻文件,例如 zzd.wav (允許帶路徑,例如: "L:\\Li\\prog\\zzd.wav" )
在程序開始播放音樂
在 while (1) { 這里 添 自己的程序 ,含程序結束的控制;}
例子:
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound (TEXT("zzd.wav"), NULL, SND_ASYNC | SND_NODEFAULT);
while (1)
{
printf("program is running... here\n");
Sleep(1000);
}
return 0;
}
❻ C語言編程mp3的播放,求解怎麼實現通過調用mp3的解碼源程序
1.你說的解碼程序一般是作為庫存在的,也就是xx.dll
2.c語言調用庫可以參考msdn或者網路,其實都很容易實現。
3.一般成熟的庫都會有官方說明,看看官方說明就會知道需要的函數在什麼dll裡面。
4.你現在的問題主要是入門不太會,搞懂了第一第二兩個方面,後面幾乎不涉及演算法,還是比較容易製作的。
祝你好運,不懂留言給我。
❼ 如何用c語言編出能用鍵盤敲出音樂的的程序
下面用beep函數發音,按鍵 1,2,3,4,5,6,7,8 對應 多來米發少。。。
按換行鍵結束。0.3秒一個音,不要按得太快。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void main()
{
char p;
int i=0;
printf("input 12345678..\n");
while ( i < 50 ){
p = getch();
if (p == '\r') break;
switch(p-'0'){
case 1: Beep(523,300);break;
case 2: Beep(587,300);break;
case 3: Beep(659,300);break;
case 4: Beep(698,300);break;
case 5: Beep(784,300);break;
case 6: Beep(880,300);break;
case 7: Beep(980,300);break;
case 8: Beep(1060,300);break;
default: break;
}
i++;
}
exit(0);
}
❽ 怎樣用C語言編程打開一個文件(比如mp3,mp4)
可以使用PlaySound()函數播放mp3音頻,該函數原型位於windows.h。
PlaySound函數的聲明為:
BOOL PlaySound(LPCSTR pszSound, HMODULE hwnd,DWORD fdwSound);
參數pszSound是指定了要播放聲音的字元串。
參數hwnd是應用程序的實例句柄,除非pszSound的指向一個資源標識符(即fdwSound被定義為SND_RESOURCE),否則必須設置為NULL。
參數fdwSound是標志的組合,如下表所示。若成功則函數返回TRUE,否則返回FALSE。
使用PlaySound函數時需要在#include<windows.h>後面加上(注意:不能加在前面):
常式:
CFileDialog dlg(TRUE, "mp3", "*.mp3", OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, "mp3文件(*.mp3)|*.mp3|");
/*創建選擇對話框,讓用戶從文件夾中選取一個MP3文件*/
int iRet = dlg.DoModal();//獲得對話框返回值
if(IDOK == iRet) //如果返回值成功,表明成功獲取一個MP3文件
{
CString pathName= dlg.GetPathName(); //得到文件的路徑名稱
PlaySound( pathName , NULL, SND_FILENAME | SND_ASYNC);//用playsound函數播放該文件
}