0
0
  1. how can I stop or pause a sound/music file from playing?
  2. If my game is going to have 3 sounds playing at once, bg music, bg sound effects and any other sounds should I have a channel for each of them?

  3. I have a sound effect that plays every time the up or down arrows are press however after awhile it will no longer play, is there any particular reason for this?

-regards Namingway

  • You must to post comments
0
0

Check out the submixing example, it loads and plays sounds in an array.

  • You must to post comments
0
0

My array is setup and working well however my problem still persists where my sound effects play 30 times and never play any sound effects again after it!

[code:1ctu1vmw]
void Sound::CreateSounds()
{
_sound = new FMOD::Sound*[4];
std::string file[4];
//create all sound effects
for(unsigned int i = 0; i < 4; i++)
{
file[i] = globalFileDirectories->GetSoundFiles()[i].file_string();
_soundEngine->createSound(file[i].c_str(), FMOD_HARDWARE, 0, &_sound[i]);
}
}

void Sound::PlaySoundEffect(int id)
{
_soundEngine->playSound(FMOD_CHANNEL_FREE, _sound[id], false, &_soundChannel);
}
[/code:1ctu1vmw]

any ideas to what the problem is? am i supposed to be freeing something up?

-thanks for your on going support ๐Ÿ˜›

  • You must to post comments
0
0
  1. Channel::setPaused( true ) should pause the sound, and Channel::setPaused( false ) should start it again from where it was paused.

  2. Yes. The typical use-case is creating a FMOD::Sound for every unique sound sample you’ll want to play. So if your game has 5 sound effects and 3 pieces of music you’ll use createSound( ) for each of them to create 8 FMOD::Sound instances total. Whenever you want to play one of your sound samples, you’ll find the FMOD::Sound you created for it and use it in a call to playSound( ) which will create an FMOD::Channel object. If you want to play the same sound effect three times at once, you’ll call playSound( ) three different times using the same Sound object, and it will create three different Channels.

  3. Not sure about this one, don’t think I’ve ever had that happen before, but maybe it will go away if you switch to the strategy in #2 (and weren’t already using it already) :)

  • You must to post comments
0
0

There doesn’t seem to be any problem with your code, and you shouldn’t have to free anything up, that stuff is done when you call System->update().

I took the two functions you posted here and made a project and it works fine, there is no limit on the number of times i can play the sound files. I think the problem lies elsewhere in your code.

<includes ommited>
[code:3jsofmhr]
FMOD::System *_soundEngine;
FMOD::Channel *_soundChannel;
FMOD::Sound **_sound;
std::string file[4];
FMOD_RESULT result;

void ERRCHECK(FMOD_RESULT checkresult)
{
if (checkresult != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", checkresult, FMOD_ErrorString(result));
}
}

void CreateSounds()
{
_sound = new FMOD::Sound*[4];
std::string file[4];
//create all sound effects
for(unsigned int i = 0; i < 4; i++)
{
file[i] = "c:\media\jaguar.wav";
_soundEngine->createSound(file[i].c_str(), FMOD_HARDWARE, 0, &_sound[i]);
}
}

void PlaySoundEffect(int id)
{
_soundEngine->playSound(FMOD_CHANNEL_FREE, _sound[id], false, &_soundChannel);
}

int main(int argc, char * argv[])
{
//used for key press events.
int key = 0;

/*
    Create a System object and intialize.
*/
ERRCHECK(result = FMOD::System_Create(&amp;_soundEngine));

ERRCHECK(result = _soundEngine-&gt;init(10, FMOD_INIT_NORMAL, 0)); // Here 10 is the number of channels that can play simultaneously

printf("TEST PROJECT FOR FORUM QUESTION \n"
"Press ‘K’ to load files\n"
"Then press ‘M’ to play");

do
{
    if( _kbhit())
    {
        key = _getch();

        if( key=='k' || key=='K')
        {
            CreateSounds(); // create sounds
     }

        if( key=='m' || key=='M')
        {
            static int index = 0;
            PlaySoundEffect(index++); // play sound effect
            index = index &gt; 3 ? 0 : index; // make sure index doesn't equal 4 or greater
     }
   }
    ERRCHECK(result = _soundEngine-&gt;update());

  Sleep(10);

}while (key!=27);
return 0;

}
[/code:3jsofmhr]

  • You must to post comments
0
0

With 3, are you re-using channels? channel_free will just grab the next one.

  • You must to post comments
0
0

๐Ÿ˜† I’m glad you pointed out the system->update() mine was not getting called and now it is and there aren’t any more problems, thanks so much! ๐Ÿ˜€

  • You must to post comments
0
0

Edit: I’ve fixed up the stopping and pausing, if you could just review my code that would be highly appreciated.
[code:2y6gzmay]
Sound::Sound()
{
_self = this;
_bgMusicChannel = 0;
_bgSFXChannel = 0;
_soundChannel = 0;
_bgMusic = 0;
_bgSFX = 0;
_sound = 0;

_result = FMOD::System_Create(&amp;_soundEngine);
ERRCHECK(_result);

_result = _soundEngine-&gt;getVersion(&amp;_version);
ERRCHECK(_result);

if (_version &lt; FMOD_VERSION)
{
    printf(&quot;Error!  You are using an old version of FMOD %08x.  This program requires %08x\n&quot;, _version, FMOD_VERSION);
}

_result = _soundEngine-&gt;getDriverCaps(0, &amp;_caps, 0, 0, &amp;_speakermode);
ERRCHECK(_result);

_result = _soundEngine-&gt;setSpeakerMode(_speakermode);
ERRCHECK(_result);

if (_caps &amp; FMOD_CAPS_HARDWARE_EMULATED)
{
    _result = _soundEngine-&gt;setDSPBufferSize(1024, 10); 
    ERRCHECK(_result);
}

_result = _soundEngine-&gt;init(100, FMOD_INIT_NORMAL, 0);
//if speaker mode isnt supported put back to stereo
if (_result == FMOD_ERR_OUTPUT_CREATEBUFFER)
{
    _result = _soundEngine-&gt;setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
    ERRCHECK(_result);

    _result = _soundEngine-&gt;init(100, FMOD_INIT_NORMAL, 0);
    ERRCHECK(_result);
}

}
Sound::~Sound()
{
_bgMusic->release();
_bgSFX->release();
_sound->release();

_soundEngine-&gt;release();

}

void Sound::PlaySoundEffect(std::string filename)
{
_soundEngine->createSound(filename.c_str(), FMOD_HARDWARE, 0, &_sound);
_soundEngine->playSound(FMOD_CHANNEL_FREE, _sound, false, &_soundChannel);
}

void Sound::SetBackgroundMusic(std::string filename, bool loop)
{
_soundEngine->createSound(filename.c_str(), FMOD_HARDWARE, 0, &_bgMusic);
_soundEngine->playSound(FMOD_CHANNEL_FREE, _bgMusic, false, &_bgMusicChannel);
_bgMusic->setLoopCount(-1);
}

void Sound::StopBackgroundMusic()
{
_bgMusicChannel->stop();
}

void Sound::SetBackgroundSoundEffect(std::string filename, bool loop)
{
_soundEngine->createSound(filename.c_str(), FMOD_HARDWARE, 0, &_bgSFX);
_soundEngine->playSound(FMOD_CHANNEL_FREE, _bgSFX, false, &_bgSFXChannel);
_bgSFX->setLoopCount(-1);
}
[/code:2y6gzmay]

Based on that what should I change improve etc and get the sounds to stop and pause.

  • You must to post comments
0
0

Glad to hear you got it all working.

  • You must to post comments
0
0

Don’t create the sound every time you want to play it. Create it once, then play it later as many times as you want.

  • You must to post comments
0
0

So would I create all of the sounds at the start in the constructor? and then play them as needed or…?

  • You must to post comments
0
0

[quote="namingway":1hzvj4yu]So would I create all of the sounds at the start in the constructor? and then play them as needed or…?[/quote:1hzvj4yu]
Correct. If you have a manageable number of sounds and can afford to have them all in memory at once (most games) then just make them all up front and save the pointers for use in calls to playSound() later.

  • You must to post comments
0
0

Okay then, I’ve changed the setmusic background to createStream instead of createSound, would I do something different in this case?

  • You must to post comments
0
0

‘streams’ can play on channels just like ‘sounds’ can. You shouldn’t have to do anything different except use
_soundEngine->createStream(filename.c_str(), FMOD_HARDWARE, 0, &_bgMusic);

The one major difference with a stream is it cannot be played multiple times at once. Since you’re using it for music this shouldn’t be a problem.

  • You must to post comments
0
0

I’m having trouble getting this done, I’m trying to create an array to create all the sounds but it just throws errors! Could you provide an example? This program is data driven and the amount of sounds/songs etc can vary…

  • You must to post comments
Showing 14 results
Your Answer

Please first to submit.