如何在c语言程序中添加背景音乐?

如何在c语言程序中添加背景音乐?,第1张

用PlaySound 函数可以播放 .wav 格式音乐。\x0d\x0a例如 下面 播放 紫竹调.wav 格式音乐,它存放在 D:\\zzz\\zzd.wav \x0d\x0a其它音乐格式能不能播放,要试验一下才知道,也许不行,也许可以。\x0d\x0a#include \x0d\x0a#include \x0d\x0a#include \x0d\x0a#pragma comment(lib, "winmm.lib")\x0d\x0a\x0d\x0avoid main(){\x0d\x0aPlaySound (TEXT("D:\\zzz\\zzd.wav"), NULL, SND_ASYNC | SND_NODEFAULT)\x0d\x0a \x0d\x0awhile (1) \x0d\x0a{\x0d\x0a printf("program is running... here\n") //这里跑你的程序,按 Ctrl-C 组合键,结束程序。\x0d\x0a Sleep(1000) //休息1秒\x0d\x0a}\x0d\x0a\x0d\x0aexit(0)\x0d\x0a}

1:功能差劲的Playsound函数

事例代码如下

#include<windows.h>

#include<stdio.h>

#pragma comment(lib,"WinMM.Lib")

void main()

{

PlaySound("c:\\dui12.wav",NULL,SND_FILENAME|SND_ASYNC)

}

2:功能和Playsound函数一样差劲的函数undplaysound

#include<windows.h>

#include<stdio.h>

#pragma comment(lib,"WinMM.Lib")

void main()

{

undPlaySound("c:\\dui12.wav",NULL,SND_FILENAME|SND_ASYNC)

}

3:功能强大的函数,支持混音的MCI类

事例代码如下

#include<windows.h>

#include "mmsystem.h"//导入声音头文件

#pragma comment(lib,"winmm.lib")//导入声音头文件库

#include<stdio.h>

void PlayMp3()

void main()

{

PlayMp3()

PlaySound("C:\\Normal2.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP)

system("pause")

}

void PlayMp3()

{

char buf[128]

char str[128] = {0 }

int i = 0

//use mciSendCommand

MCI_OPEN_PARMS mciOpen

MCIERROR mciError

//SetWindowText(NULL,"12345")

mciOpen.lpstrDeviceType = "mpegvideo"

mciOpen.lpstrElementName = "c:\\WELCOME.WAV"

mciError = mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD)&mciOpen)

if(mciError)

{

mciGetErrorString(mciError,buf,128)

printf("send MCI_OPEN command failed:%s\n",buf)

return

}

UINT DeviceID = mciOpen.wDeviceID

MCI_PLAY_PARMS mciPlay

mciError = mciSendCommand(DeviceID,MCI_PLAY,0 ,(DWORD)&mciPlay)

if(mciError)

{

printf("send MCI_PLAY command failed\n")

return

}

}

我们知道,音乐是音高和音长的有序组合,设计微机音乐最重要的就是如何定义音高和音长,以及如何让扬声器发出指定的音符。下面给出音符与频率的关系表。C语言提供的三个函数sound( )、nosound( )和clock( )可以很方便地解决上述的问题。sound( )函数可以用指定频率打开PC机扬声器直到用nosound( )函数来关闭它; clock( )函数正好用来控制发声时间,而且它不受PC机主频高低的影响。下面这段程序可使微机发出c调1的声音。

音符与频率关系表

音符 c d e f g a b

1 2 3 4 5 6 7

频率 262 294 330 349 392 440 494

音符 c d e f g a b

1 2 3 4 5 6 7

频率 523 587 659 698 784 880 988

音符 c d e f g a b

1 2 3 4 5 6 7

频率 1047 1175 1319 1397 2568 1760 1976

#include<stdio.h>

#include<dos.h>

void pause(int)

void sound1(int,int)

void main(void)

{

int i,freq,speed=5

int time=4*speed

char *qm="iddgwwwQQgfff dddfghhhggg ddgwwwqqgfff\

ddffhjqqqqq wpggjhgddgqq hhqwwqjjjggg\

ddgwwwqqqgfff ddffhjqqqqqq"/*定义歌曲*/

while (*qm++ !='\0'){

i=1

switch(*qm){

case 'k':

time=1*speedi=0

break

case 'i':

time=6*speedi=0

break

case 'o':

time=10*speedi=0

break

case 'p':

pause(time)i=0

break

case 'a':

freq=523

break

case 's':

freq=587

break

case 'd':

freq=659

break

case 'f':

freq=698

break

case 'g':

freq=784

break

case 'h':

freq=880

break

case 'j':

freq=988

break

case 'z':

freq=262

break

case 'X':

freq=294

break

case 'c':

freq=330

break

case 'v':

freq=349

break

case 'b':

freq=392

break

case 'n':

freq=440

break

case 'm':

freq=494

break

case 'q':

freq=1047

break

case 'w':

freq=1175

break

case 'e':

freq=1319

break

case 'r':

freq=1397

break

case 't':

freq=2568

break

case 'y':

freq=1760

break

case 'u':

freq=1976

break

default:

i=0

break

}

if(i)

sound1(freq,time)

}

}

void sound1(int freq,int time) /*freq为频率,time为持续时间*/

{

union{

long divisor

unsigned char c[2]

}count

unsigned char ch;

count.divisor=1193280/freq/* 1193280 是系统时钟速率*/

outp(67,182)

outp(66,count.c[0])

outp(66,count.c[1])

ch=inp(97)

outp(97,ch|3)

pause(time)

outp(97,ch)

}

void pause(int time)

{

int t1,t2

union REGS in,out

in.h.ah=0X2c

int86(0X21,&in,&out)/* 取当前时间*/

t1=t2=100*out.h.dh+out.h.dl/*out.h.dh 为秒值,out.h.dl 为1/100 秒值*/

while(t2-t1<time)

{

int86(0X21,&in,&out)

t2=100*out.h.dh+out.h.dl

if (t2<t1)t2+=6000/* 增加一分钟*/

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11353703.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存