Allegro sample/stream
larzconwell

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.

larzconwell

Okay here's how its working

if (bg_music_on == false) {
al_stop_samples();
al_play_sample(bg_music, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
bg_music_on = true;
} else {
al_stop_samples();
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().

SiegeLord

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);
al_play_sample_instance(bg_music_instance);

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

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

larzconwell

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

Edgar Reynaldo
SiegeLord

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.

larzconwell

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

Thread #608940. Printed from Allegro.cc