Search code examples
caudioallegroallegro5

Playing sound file in C with Allegro5


I am trying to play wav file in C with allegro5 and I wrote below code:

#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;
}

I debuged this in visual studio 2013 and sample always NULL. I am tried some variety format.

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

and etc.

I am confused. How can I succeed this problem. Thanks in advance. Sorry language.


Solution

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

    It is working for me.