Jump to content

Playing a sound?


Recommended Posts

Edit: Holy sh*t I'm an idiot. Apparently "sound" in the constructor both times means the argument, not the private member :/

I updated the class in this text so anybody else can use it. Should have protection against playing multiple times too, but I don't know what's gonna happen if you make multiple instances.

 

Edit 2: Updated the destructor. Stopping and releasing the sound without checking stuff will cause the game to crash.

That wasn't fun finding :/

 

Hi there!

I've been trying to get a sound to play, but it either starts and doesn't stop, or it doesn't start.

 

I played around with AUDIO::PLAY_SOUND_FROM_ENTITY and got it to sort of kind of work, but I couldn't stop it properly. Then I decided to borrow CamxxCore's sound class, and then sound didn't play. Afaik there isn't much different about the calls?

 

C# class https://github.com/CamxxCore/AirSuperiority/blob/master/Script/GameSound.cs

 

C++ class:

.h

 

class GameSound {public:    GameSound(char *sound, char *soundSet);    ~GameSound();    void Load(char *audioBank);    void Play(Entity ent);    void Stop();    bool Active;private:    char *m_soundSet;    char *m_sound;    int m_soundID;    int m_prevNotification;};
.cpp

 

GameSound::GameSound(char *sound, char *soundSet) {    Active = false;    m_sound = sound;    m_soundSet = soundSet;    m_soundID = -1;}GameSound::~GameSound() {    if (m_soundID == -1 || !Active) return;    AUDIO::RELEASE_SOUND_ID(m_soundID);}void GameSound::Load(char *audioBank) {    AUDIO::REQUEST_SCRIPT_AUDIO_BANK(audioBank, false);}void GameSound::Play(Entity ent) {    if (Active) return;    m_soundID = AUDIO::GET_SOUND_ID();    //showNotification(("New soundID: " + std::to_string(m_soundID)).c_str(), nullptr);    AUDIO::PLAY_SOUND_FROM_ENTITY(m_soundID, m_sound, ent, m_soundSet, 0, 0);    Active = true;}void GameSound::Stop() {    if (m_soundID == -1 || !Active) return;    AUDIO::STOP_SOUND(m_soundID);    Active = false;}
What does work:

 

 

 

int soundID;//GameSound misshiftSound("DAMAGED_TRUCK_IDLE", 0); // plenty of other stuff if (startTrigger) {            soundID = AUDIO::GET_SOUND_ID();            AUDIO::PLAY_SOUND_FROM_ENTITY(soundID, "DAMAGED_TRUCK_IDLE", vehicle, 0, 0, 0);            //misshiftSound.Play(vehicle);} if (stopTrigger) {            AUDIO::STOP_SOUND(soundID);            //misshiftSound.Stop();}
The commented out code is what would've been called if I used the GameSound class. I can't really see a difference, as both GET_SOUND_ID would've updated soundID in the class. They're the same, but don't work.

 

Printing out GameSound.soundID does show it increment just like when calling it from main. I don't get it.

Edited by ikt
Link to comment
https://gtaforums.com/topic/885969-playing-a-sound/
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 0 User Currently Viewing
    0 members, 0 Anonymous, 0 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.