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

时间:2015-05-14 12:12:18

标签: 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");

等等。

我很困惑。我怎样才能成功解决这个问题。提前致谢。抱歉语言。

1 个答案:

答案 0 :(得分:1)

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

这对我有用。