【问题标题】:How to play sound effect or Music in C?如何在 C 中播放音效或音乐?
【发布时间】:2021-02-08 17:19:51
【问题描述】:

我正在制作游戏,我必须添加一些音效和音乐。

我用谷歌搜索了一下,发现了流动的代码:

#include <conio.h>
#include "inc/fmod.h"

FSOUND_SAMPLE* handle;

int main ()
{
   // init FMOD sound system
   FSOUND_Init (44100, 32, 0);

   // load and play mp3
   handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
   FSOUND_PlaySound (0,handle);

   // wait until the users hits a key to end the app
   while (!_kbhit())
   {
   }

   // clean up
   FSOUND_Sample_Free (handle);
   FSOUND_Close();
}

但是当我编译它时,我得到了流动的错误:

➜  Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
         ^~~~~~~~~
1 error generated.

【问题讨论】:

  • conio.h 不适用于 gcc,请使用 curses.h 代替 stackoverflow.com/questions/8792317/…
  • _kbhit() 也是一个 MS 函数。可能重复; How do I port this program from conio to curses?
  • 不幸的是,它是特定于操作系统的。
  • 我能想到的最基本的方式:putchar('\a'); /* and maybe */fflush(stdout);
  • 显然这段代码已经很多很多年了。根据您的目标系统(顺便说一句,那是哪一个?)您需要研究其他方式,例如播放 WAV 或 MP3。

标签: c soundeffect


【解决方案1】:

嗯,首先, 是一个 C++ 库,而您正在使用 C 进行编程。这是不同的! 然后,我记得我多年前写的一段C代码,ma​​in.c得到了如下代码(cmets是意大利文,因为我是意大利人):

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}

然后,这是另一个源文件,名为 file.c

 #include <stdio.h>
 #include <stdlib.h>
 #include <windows.h>
 #include "header.h"
 void scala1(unsigned char x){
 if(x=='a')beep(131,50);
 if(x=='s')beep(147,50);
 if(x=='d')beep(165,50);
 if(x=='f')beep(175,50);
 if(x=='g')beep(196,50);
 if(x=='h')beep(220,50);
 if(x=='j')beep(247,50);
 if(x=='k')beep(262,50);
 if(x=='l')beep(294,50);
 if(x=='w')beep(139,50);
 if(x=='e')beep(156,50);
 if(x=='r')beep(185,50);
 if(x=='t')beep(208,50);
 if(x=='y')beep(233,50);    
 }
 void scala2(unsigned char x){
 if(x=='a')beep(262,50);
 if(x=='s')beep(294,50);
 if(x=='d')beep(330,50);
 if(x=='f')beep(349,50);
 if(x=='g')beep(392,50);
 if(x=='h')beep(440,50);
 if(x=='j')beep(494,50);
 if(x=='k')beep(523,50);
 if(x=='l')beep(587,50);
 if(x=='w')beep(277,50);
 if(x=='e')beep(311,50);
 if(x=='r')beep(370,50);
 if(x=='t')beep(415,50);
 if(x=='y')beep(466,50); 
 }
 void scala3(unsigned char x){
 if(x=='a')beep(523,50);
 if(x=='s')beep(587,50);
 if(x=='d')beep(659,50);
 if(x=='f')beep(698,50);
 if(x=='g')beep(784,50);
 if(x=='h')beep(880,50);
 if(x=='j')beep(988,50);
 if(x=='k')beep(1046,50);
 if(x=='l')beep(1175,50);
 if(x=='w')beep(554,50);
 if(x=='e')beep(622,50);
 if(x=='r')beep(740,50);
 if(x=='t')beep(831,50);
 if(x=='y')beep(932,50);  
 }

最后一个,文件 header.h。代码如下:

void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);

所有源文件必须位于同一目录中。你编译 main.c,然后你只需要按 a,s,d,..y 和 1,2,3。尝试!它可以工作,当然如果你想更改部分代码,你可以做到。我希望你喜欢我的节目,这很有趣:)

【讨论】:

  • firstly &lt;conio.h&gt; is a C++ library 不,不是。
  • 对,但通常在创建 C 代码时会添加以下库:跨度>
  • 那是...无关的? conio.h 是一个 DOS 库,用于连接终端(CONsole 输入/输出)。它不是可移植的,仅在 DOS 和早期 Windows 中使用。 stdio.hstdlib.hvery standard C 标准库的一部分。所以...是的,如果我编写执行一些 I/O 并分配内存的 C 代码,通常我会添加 stdio.hstdlib.h
  • 您介意格式化您的代码吗? ——你认为这适用于现有系统吗?你不知道 OP 使用的是哪一个。
  • 嗯,我在 windows 7 和 windows 10 上使用过它。对于其他操作系统,您必须更改函数 system()。其他代码应该可以正常工作
猜你喜欢
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
相关资源
最近更新 更多