Allegro sample/stream

Okay in my game, I have background music, currently it is in: 'al_play_sample(bg_music, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);'

but when I pause the game I want the sample to pause.

How could I do that? Or do I have to make a stream! and If so how do I make a stream?

Edgar Reynaldo

The manual is your friend.


Okay here's how its working

if (bg_music_on == false) {
al_play_sample(bg_music, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
bg_music_on = true;
} else {
bg_music_on = false;

When I press enter it runs through that if..else, bg_music_on is false at first.

When I press enter again and it starts the sample, it restarts it. How do I make it pause? or turn the gain down

Matthew Leverton

The simple API that you are using doesn't support that sort of thing. If you want to micro-manage, you'll need to load sample data and create a sample instance and use al_set_sample_instance_playing().


In this case you're better of not using the simple audio API, but using the more advanced API.

You'd do something like this:

/* Level initialization */
ALLEGRO_SAMPLE_INSTANCE* bg_music_instance = al_create_sample_instance(bg_music);
al_attach_sample_instance_to_mixer(bg_music_instance, al_get_default_mixer());
al_set_sample_instance_playmode(bg_music_instance, ALLEGRO_PLAYMODE_LOOP);

/* Game paused */
al_set_sample_instance_playing(bg_music_instance, false);

/* Game un-paused */
al_set_sample_instance_playing(bg_music_instance, true);


That works great! Thanks!! Just it still, restarts the song.

Edgar Reynaldo

It does? That's odd. Well, then you can save the spot you're in the music before you stop it using al_get_sample_instance_position and then do al_set_sample_instance_position after you restart it.


Instead of using 'al_set_sample_instance_playing' I used 'al_set_sample_instance_gain', It works good enough!

Thread #608940. Printed from