How to stop a sample in A5?
ISDcaptain

I remember in A4 all you had to do was call:
stop_sample(sampleName); done.

In A5 its asking for a sample ID?
What is that? Why cant it be like A4?
Someone help me out.

Matthew Leverton
ALLEGRO_SAMPLE_ID my_id;
al_play_sample(..., &my_id);
al_stop_sample(&my_id);

It won't really make sense until you understand the relationship between an ALLEGRO_SAMPLE and an ALLEGRO_SAMPLE_INSTANCE.

But in short, think about:

ALLEGRO_SAMPLE *my_sample;

// play sample three times consecutively
al_play_sample(my_sample, ...);
al_play_sample(my_sample, ...);
al_play_sample(my_sample, ...);

al_stop_sample(my_sample); // Doesn't work. (Which of the above three should it stop?)

ISDcaptain

Okay I try that, and when I click a button to call the al_stop_sample(&id);
my program crashes

Matthew Leverton

Post relevant code. (Make sure you've got ALLEGRO_SAMPLE_ID id and not ALLEGRO_SAMPLE_ID *id;).

ISDcaptain

#SelectExpand
1 2 3int main() 4{ 5 al_init(); 6 if(!al_init()) 7 { 8 al_show_native_message_box(NULL, NULL, NULL, "Initialization Error", NULL, NULL); 9 } 10 11 //Display 12 ALLEGRO_DISPLAY *display; 13 al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE); 14 display = al_create_display(WIDTH, HEIGHT); 15 al_set_window_title(display, "Music Test"); 16 17 //Initialize the addons 18 al_install_keyboard(); 19 al_install_audio(); 20 al_init_ttf_addon(); 21 al_init_font_addon(); 22 al_init_image_addon(); 23 al_init_acodec_addon(); 24 ALLEGRO_KEYBOARD_STATE keyState; 25 26 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 27 ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS); 28 al_register_event_source(event_queue, al_get_display_event_source(display)); 29 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 30 al_register_event_source(event_queue, al_get_keyboard_event_source()); 31 32 33 //Game Variables 34 bool done = false; 35 ALLEGRO_BITMAP *background = al_load_bitmap("background.png"); 36 ALLEGRO_SAMPLE *overworld = al_load_sample("overworld.wav"); 37 ALLEGRO_SAMPLE_ID id; 38 al_reserve_samples(1); 39 40 al_start_timer(timer); 41 42 while(!done) 43 { 44 ALLEGRO_EVENT events; 45 al_wait_for_event(event_queue, &events); 46 47 if(events.type == ALLEGRO_EVENT_TIMER) 48 { 49 al_get_keyboard_state(&keyState); 50 if(al_key_down(&keyState, ALLEGRO_KEY_UP)) 51 { 52 al_play_sample(overworld, 1.0, 0, 1, ALLEGRO_PLAYMODE_LOOP, &id); 53 } 54 if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)) 55 { 56 al_stop_sample(&id); 57 } 58 59 } 60 61 al_flip_display(); 62 63 } 64 65 return 0; 66}

Matthew Leverton

That shouldn't crash. (Be sure to check the return codes for all of your loading functions to make sure nothing is returning NULL.)

However, assuming FPS is sufficiently high, when you press up, you will trigger multiple calls to al_play_sample() by a single key press. Because you set al_reserve_samples() to 1, you won't notice that ... i.e., you can only play a single sound at a time. But that scenario still modifies the id variable and will cause the subsequent call to al_stop_sample() to fail.

So if you set FPS to 1.0 and hold the UP key until you hear a sound, that will trigger exactly one call to al_play_sample(). Then if you hold the DOWN key, it should eventually stop (within a second).

If that still crashes, you should upgrade to the latest version of Allegro and then try to step through the code with a debugger to see where it crashes.

Unrelated: your last parameter to the show dialog method should be an integer, not NULL.

ISDcaptain

My fps is set to 60

#SelectExpand
1 2This is where the compiler sets a breakpoint at: 3 if (rterrnum != _RT_CRNL && rterrnum != _RT_BANNER && rterrnum != _RT_CRT_NOTINIT) 4 { 5 switch (_CrtDbgReportW(_CRT_ERROR, NULL, 0, NULL, error_text)) 6 { 7 case 1: _CrtDbgBreak(); msgshown = 1; break; 8 case 0: msgshown = 1; break; 9 } 10 }

Thomas Fjellstrom

Yeah, you'll want to scan up the backtrace till you find the actual crash point, and not the internal debugger stuff.

duncan perham

could be wrong here, but it looks like you stop the sound on a key down and start it on a key up...
I expect when you press the key for the first time, it tries to stop nothing, the id is null as it hasnt been initilised???

Read it a bit more, above is wrong, but you are in a loop, how many times does the event keydown pass, stopping a stopped sound is probably not good, also, you may be staring dozens of new sounds???

try
al_play_sample(overworld, 1.0, 0, 1, ALLEGRO_PLAYMODE_LOOP, &id);
LastTime=al_get_timer_count(timer);

wait for 10 secs (using the allegro timer),then stop it.
ThisTime=al_get_timer_count(timer);
if(ThisTime-LastTime>10*60)

Thread #613718. Printed from Allegro.cc