c - 使用 Allegro5 在 C 中播放声音文件

标签 c audio allegro allegro5

我想用 allegro5 在 C 中播放 wav 文件,我写了下面的代码:

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>

int main(int argc, char **argv){

    ALLEGRO_DISPLAY *display = NULL;


    if (!al_init()){
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
    }

    if (!al_install_audio()){
        fprintf(stderr, "failed to initialize audio!\n");
        return -1;
    }

    if (!al_init_acodec_addon()){
        fprintf(stderr, "failed to initialize audio codecs!\n");
        return -1;
    }

    if (!al_reserve_samples(1)){
        fprintf(stderr, "failed to reserve samples!\n");
        return -1;
    }


    al_install_audio();
    al_init_acodec_addon();

    ALLEGRO_SAMPLE *sample = al_load_sample("bomb.wav"); //sample always NULL

    al_reserve_samples(1);

    if (!sample){
        printf("Audio clip sample not loaded!\n");
        return -1;
    }

    display = al_create_display(640, 480);

    if (!display){
        fprintf(stderr, "failed to create display!\n");
        return -1;
    }

    /* Loop the sample until the display closes. */
    al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);

    al_rest(10.0);

    al_destroy_display(display);
    al_destroy_sample(sample);
    return 0;
}

我在 visual studio 2013 中对此进行了调试,样本始终为 NULL。 我尝试了多种格式。

例如:ALLEGRO_SAMPLE *sample = al_load_sample("\\bomb.wav");

ALLEGRO_SAMPLE *sample = al_load_sample("Resource Files\\bomb.wav");

ALLEGRO_SAMPLE *sample = al_load_sample("\\Resource Files\\bomb.wav");

ALLEGRO_SAMPLE *sample = al_load_sample("C:\\bomb.wav");//after copied in C drive

ALLEGRO_SAMPLE *sample = al_load_sample("C:/bomb.wav");

等等

我很困惑。我怎样才能成功这个问题。提前致谢。对不起语言。

最佳答案

ALLEGRO_SAMPLE *sample = al_load_sample("bomb.ogg"); //convert to ogg file your link

它对我有用。

关于c - 使用 Allegro5 在 C 中播放声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30237117/

相关文章:

c++ - 将 Qt Creator 与 Allegro 结合使用

在虚拟机上运行时 cURL MIME 数据损坏

c - 将字符串传递给 main 并分解为数组

audio - 是否可以使用结束的声音文件过渡到新场景或刷新同一场景中的数据?

c++ - C++中的MIDI音符消息

c++ - 错误: taking address of temporary [-fpermissive]

java - C、java中如何产生cpu缓存效果?

c - 使用结构体指针的源代码

android - Android录音问题

c - 在 OSX 上运行 Allegro 时出错