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

Author Topic: Audio Issue  (Read 3828 times)

0 Members and 1 Guest are viewing this topic.

NewZombieland

  • Newbie
  • *
  • Posts: 2
    • View Profile
Audio Issue
« on: August 02, 2012, 01:32:49 am »
Hello, I'm attempthing to put audio into my SFML 2.0 game and have hit a bit of a road-block.
I can play audio fine my my games main function with this code:
sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("sound file name"))
    {
        std::cout<<"failed to load sound"<<std::endl;
    }
   
    sf::Sound sound(buffer);
    sound.play();

but I want another class, PlaySoundEvent to be able to trigger sounds, but this just dosent work.

PlaySoundEvent is a fairly simple class, you pass it a buffer and then call the fire(), witch just creates a sf::Sound with the supplied buffer and then plays it.

so the code:

sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("sound file name"))
    {
        std::cout<<"failed to load sound"<<std::endl;
    }
   
    PlaySoundEvent sndEvent(buffer);
    sndEvent.fire();

should work exactly the same as above, but it dosen't.
Have I massed up my includes?

PlaySoundEvent .fire() looks like this:
void PlaySoundEvent::fire()
{
    sf::Sound soundObj(sound);
    soundObj.setVolume(100);
    soundObj.play();
}
« Last Edit: August 02, 2012, 11:21:04 pm by NewZombieland »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Audio Issue
« Reply #1 on: August 02, 2012, 03:35:15 am »
Welcome to the forum. It's mostly adviced to first look a bot around when arriving in a new place, so one could've noticed that everyone uses code tahs for their source code (code=cpp), or to provide more information, that "it doesn't work". ;)
Does it crash? Does just not play the sound? Does the console print out something?
Also what the signature of your class?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NewZombieland

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Audio Issue
« Reply #2 on: August 02, 2012, 11:25:51 pm »
No console errors, though when I call the sounds getStatus() it returns 2 (Sound::Playing).
It just dosen't play.
here's the hedder for the class:

#ifndef PLAYSOUNDEVENT_H_
#define PLAYSOUNDEVENT_H_


#include <SFML/Audio.hpp>

#include "Event.h"
class PlaySoundEvent: public Event
{
    sf::SoundBuffer sound;
public:
        PlaySoundEvent(sf::SoundBuffer sound);
        virtual ~PlaySoundEvent();
        void fire();
};

#endif /* PLAYSOUNDEVENT_H_ */

Cheers

garthos

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Audio Issue
« Reply #3 on: October 27, 2012, 09:40:06 am »
I'm still learning the audio library myself, and are using 1.6 not 2, but heres my 2 cents.

A trap I seem to fall into is not giving the sound any time to play. I assume it goes out of scope before it has played. So you need a loop to keep it alive after calling .Play()

while (mySound.GetStatus() == sf::Sound::Playing)
    {
        // Display the playing position
       // std::cout << "\rPlaying... " << std::fixed << soundSprite.GetPlayingOffset() << " sec\n";
       // Leave some CPU time for other threads
        sf::Sleep(0.1f);
    }

This is a better description quoted from http://en.sfml-dev.org/forums/index.php?topic=6342.0
No you don't need this loop to play the sound, it is played in a separate thread so the main one can do whatever it wants.
You just need to make sure that the instance lives as long as you're playing it. If it is destroyed before, obviously the music will stop ;)
« Last Edit: October 27, 2012, 11:44:45 am by garthos »