Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » sound problems... again

This thread is locked; no one can reply to it. rss feed Print
sound problems... again
alex Ioan
Member #12,015
June 2010

read the tutorial but didn't find a good way around this...

sound.cpp

#SelectExpand
1 2#include"sound.h" 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_audio.h> 5#include <allegro5/allegro_acodec.h> 6 ALLEGRO_SAMPLE_ID* bkgid; 7 ALLEGRO_SAMPLE_ID* bkgfid; 8 9void sound::startbackmusic() 10 {if(musicon==1) 11 { bkg=al_load_sample("data/bkg.wav"); al_play_sample(bkg,0.55,0,1.0,ALLEGRO_PLAYMODE_LOOP,bkgid);}} 12 13 14void sound::stopbackmusic(int a) 15 {if(musicon==1) 16 {al_stop_sample(bkgid); 17 if (a==1) 18 {al_destroy_sample(bkg);} 19 } 20 }

sound.h

#SelectExpand
1 2#pragma once 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_audio.h> 5#include <allegro5/allegro_acodec.h> 6 7class sound 8{ public: 9sound(): 10 soundon(1), 11 musicon(1), 12 13 crit(al_load_sample("data/crit.wav")), 14 deagle(al_load_sample("data/deagle.wav")), 15 attack(al_load_sample("data/attack.wav")) 16//loading different sounds 17 {} 18 void startbackmusic(); 19 void stopbackmusic(int a); 20 21 22 23private: 24 25ALLEGRO_SAMPLE *bkg; 26//+ all other 27int soundon; 28int musicon; 29 30};

so when I call sound.stopbackmusic(1); it just doesnt work, gives me "access violation reading..." and points to "al_stop_sample(bkgid);"

what am I doing wrong here ? :'(

The only work around I can find is to destroy the sample directly.

PS:

simple code in main.cpp

ALLEGRO_SAMPLE_ID * bkgid;
ALLEGRO_SAMPLE *bkg;

bkg=al_load_sample("data/bkg.wav");
al_play_sample(bkg,1.0,0,1.0,ALLEGRO_PLAYMODE_LOOP,bkgid); 
Sleep(1500);
al_stop_sample(bkgid);

tells me that bkgid is not initialized

Trent Gamblin
Member #261
April 2000
avatar

If I'm not mistaken then you should be passing already allocated ALLEGRO_SAMPLE_IDs. You could change them from pointers to regular variables and pass the address.

alex Ioan
Member #12,015
June 2010

that....worked !

But how could one know about those default sample_id's ?

anyway this is the correct way to stop a sample ? make it an ID and pass it's address ?

Trent Gamblin
Member #261
April 2000
avatar

Yes it is.

Edgar Reynaldo
Member #8,592
May 2007
avatar

alex Ioan
Member #12,015
June 2010

Yea sorry but I'm stupid again...

I want to make a simple thing... no loops needed in my opinion:

Display text. If the user presses any keys, clear and display some other text...

I can't make it work

#SelectExpand
1 2if (player.introcheck()==0) 3{ 4al_clear_to_color(al_map_rgb(0,0,0)); 5//text 6 7al_wait_for_event(event_queue, &ev); 8if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 9{al_flush_event_queue(event_queue); 10al_clear_to_color(al_map_rgb(0,0,0)); 11//more text 12 } 13 14 15al_wait_for_event(event_queue, &ev); 16if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 17{al_flush_event_queue(event_queue); 18player.introset(); 19} 20}

Edgar Reynaldo
Member #8,592
May 2007
avatar

If you have a timer registered to your event queue, then you won't ever get your ALLEGRO_EVENT_KEY_DOWN event, and it will just skip past those checks. You need to wait in a loop until the event is an ALLEGRO_EVENT_KEY_DOWN event.

void WaitForKeyPress(ALLEGRO_EVENT_QUEUE* queue) {
   while (1) {
      ALLEGRO_EVENT ev;
      al_wait_for_event(queue , &ev);
      if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {break;}
   }
}

alex Ioan
Member #12,015
June 2010

done it ! thanks for the tip !

Go to: