Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: My Sonnd doesn't play  (Read 3810 times)

0 Members and 1 Guest are viewing this topic.

Anis

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
My Sonnd doesn't play
« on: July 12, 2024, 10:02:23 pm »
Hi everyone, i have a proplem, my sound doesn't play in my program, i have the message "song played" but the program still doesn't play the song, i already checked my own sound devices, and the soundBuffer and the Sound object are variables of the class, so i don't think that is a variable scope problem. so here is the code, if someone have an idea.


class TBIbutton
        {
               
        public:
                TBIbutton(std::string txtBTN, uint16_t numberTypeAnimation = uint16_t(4), std::shared_ptr<sf::SoundBuffer> bufferSoundBtnHovered = nullptr, std::shared_ptr<sf::Sound> soundBtnHovered = nullptr, std::shared_ptr<sf::SoundBuffer> bufferSoundBtnClicked = nullptr, std::shared_ptr<sf::Sound> soundBtnClicked = nullptr);
                virtual ~TBIbutton();
               
        private:
               
                // sounds
                sf::SoundBuffer bufferSoundWhenBTNHovered;
                sf::Sound soundWhenBTNHovered;
                sf::SoundBuffer bufferSoundWhenBTNClicked;
                sf::Sound soundWhenBTNClicked;

                bool ifPlaySoundWhenBtnHovered;
                bool ifPlaySoundWhenBtnClicked;
                bool ifAlreadyPlayedSoundBTNHovered;
                bool ifAlreadyPlayedSoundBTNClicked;

                bool ifBTNcurrentlyClick;

        };

 
constructor of TBIbutton:
TBIbutton::TBIbutton(std::string txtBTN, uint16_t numberTypeAnimation, std::shared_ptr<sf::SoundBuffer> bufferSoundBtnHovered, std::shared_ptr<sf::Sound> soundBtnHovered, std::shared_ptr<sf::SoundBuffer> bufferSoundBtnClicked, std::shared_ptr<sf::Sound> soundBtnClicked)
{
   

   // sound
   this->ifPlaySoundWhenBtnHovered = true;
   this->ifPlaySoundWhenBtnClicked = true;
   this->ifAlreadyPlayedSoundBTNHovered = false;
   this->ifAlreadyPlayedSoundBTNClicked = false;
   this->ifBTNcurrentlyClick = false;

   if (bufferSoundBtnHovered != nullptr)
   {
      bufferSoundWhenBTNHovered = *bufferSoundBtnHovered;
      soundWhenBTNHovered = *soundBtnHovered;
   }
   else
   {
      this->ifPlaySoundWhenBtnHovered = false;
   }

   if (bufferSoundBtnClicked != nullptr)
   {
      bufferSoundWhenBTNClicked = *bufferSoundBtnClicked;
      soundWhenBTNClicked = *soundBtnClicked;
   }
   else
   {
      this->ifPlaySoundWhenBtnClicked = false;
   }
}


 
void TBIbutton::draw(sf::RenderWindow& window, std::chrono::duration<double>& clock, sf::Event& event, sf::Vector2i mousePos, ValuesAnimation& values, bool& ifAnimationOpacity)
{
                if (ifBtnClick<TBIbutton>(*this, event, mousePos))
                {
                        if (this->ifAlreadyPlayedSoundBTNHovered == false && this->ifPlaySoundWhenBtnHovered == true && this->ifBTNcurrentlyClick == false)
                        {
                                std::cout << "song PLAYED !!!" << std::endl;
                                soundWhenBTNHovered.play();
                                this->ifAlreadyPlayedSoundBTNHovered = true;   
                        }
                        ifBTNcurrentlyClick = true;
                }
               
                        }
                }
                if (!ifBtnClick<TBIbutton>(*this, event, mousePos) && values.IfAnimationColorFinish == true)
                {
       
                {
                        if (this->ifBTNcurrentlyClick == true) // if we don&#39;t click on the btn
                        {
                                this->ifBTNcurrentlyClick = false;
                                this->ifAlreadyPlayedSoundBTNHovered = false;
                                this->ifAlreadyPlayedSoundBTNClicked = false;
                        }
                }
       
}

 

and here is when we make an instance of this class


// sound btn
                                                        std::shared_ptr<sf::SoundBuffer> soundbufferBtn = std::make_shared<sf::SoundBuffer>();
                                                        std::shared_ptr<sf::Sound> soundBtn = std::make_shared<sf::Sound>();
                                                        if (!soundbufferBtn->loadFromFile("Resources/sounds/SelectBtn.wav"))
                                                        {
                                                                std::cout << "erreur load sound" << std::endl;
                                                        }
                                                        soundBtn->setBuffer(*soundbufferBtn);
                                                        soundBtn->setVolume(100);

btn_buyOneOrSeveralsShares.push_back(std::make_unique<TheBestInvestorInterface::TBIbutton>("Buy company shares", size_t(4), soundbufferBtn, soundBtn));

 
« Last Edit: July 12, 2024, 10:06:34 pm by Anis »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: My Sonnd doesn't play
« Reply #1 on: July 14, 2024, 11:09:40 pm »
My guess is it's this part:
bufferSoundWhenBTNClicked = *bufferSoundBtnClicked;
soundWhenBTNClicked = *soundBtnClicked;

That's taking a sound buffer pointer and sound pointer, dereferencing them and duplicating them into two class members.
The problem is the duplicate sound's internal buffer pointer is still pointing at the original buffer (which will be cleaned up once the shared pointer is no longer in use), not the duplicate buffer.

Maybe try calling setBuffer on the class members after assigning them.

Anis

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: My Sonnd doesn't play
« Reply #2 on: July 15, 2024, 12:48:24 am »
I solved the problem using another class and i use it with callBack and it work, thank you for your suggestions