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

Author Topic: Access violation when trying to play music in the wrapper class.  (Read 2081 times)

0 Members and 1 Guest are viewing this topic.

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Access violation when trying to play music in the wrapper class.
« on: February 18, 2018, 01:24:45 pm »
Hello all. I'm wrapping sf::SoundBuffer and sf::Sound classes in a class named SoundObject. It is followed like this:

SoundObject.h
#ifndef SOUNDOBJECT_H
#define SOUNDOBJECT_H

#include <string>
#include <SFML/Audio.hpp>

class SoundObject {
public:
        SoundObject();
        bool Create(std::string filename);
        void Play();
private:
        sf::Sound soundInstance;
        sf::SoundBuffer soundBuffer;
};

#endif

 

SoundObject.cpp
#include "SoundObject.h"
#include <string>
#include "../GameProperties/GameProperties.h"

SoundObject::SoundObject() {

}

bool SoundObject::Create(std::string filename) {
        if (!soundBuffer.loadFromFile(filename)) {
                return false;
        }

        soundInstance.setBuffer(soundBuffer);

        return true;
}

void SoundObject::Play() {
        soundInstance.setVolume(GameProperties::GetSFXVolume());
        soundInstance.play();
}
 

And I'm calling these functions in a class like this:
if (!changeSound.Create("Sounds/System/sound_change.ogg")) {
                return false;
        }

        if (!selectSound.Create("Sounds/System/sound_select.ogg")) {
                return false;
        }
 

changeSound and selectSound is member of a class.

But it gives access violation error. When I try to play the sounds in the main function, it doesn't give error. I don't get it. Can you help me?

Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Access violation when trying to play music in the wrapper class.
« Reply #1 on: February 18, 2018, 08:06:48 pm »
And what's the exact error message?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Access violation when trying to play music in the wrapper class.
« Reply #2 on: February 18, 2018, 08:23:04 pm »
I don't get it. Now it works like charm. Thank you.  ???

 

anything