Answered
0
0

Hello, I am using fmodapi44457linux and I am interested in the playsound example. I am making some changes to it so I can use it in my code in order to play one sound only once when I call the right function. The code I am using is the code below and I am compliling it with the makefile provided. The problem I am dealing with is that if I remove the do-while loop the sound never plays. If I keep the do-while loop running with while(true) the sound plays only one time but the program never ends. After some tests I found that with i<822000 in the while this specific sound plays only one time and the program closes right after that. Can someone inform me what is this value and how can I find it so I can use this code dynamically for other sounds? Or else can someone propose another code that can accomplish the thing I am trying? Thank you.

#include "../../api/inc/fmod.hpp"
    #include "../../api/inc/fmod_errors.h"
    #include "../common/wincompat.h"
    #include <stdio.h>
    #include <iostream>
    #include <string>
    using namespace std;

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


int main(int argc, char *argv[])
{
    FMOD::System     *system;
    FMOD::Sound      *sound1;
    FMOD::Channel    *channel = 0;
    FMOD_RESULT       result;
    unsigned int      version;

    /*
        Create a System object and initialize.
    */
    result = FMOD::System_Create(&system);
    ERRCHECK(result);

    result = system->getVersion(&version);
    ERRCHECK(result);

    if (version < FMOD_VERSION)
    {
        printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
        getch();
        return 0;
    }
            int          channels = 2;
    result = system->setOutput(FMOD_OUTPUTTYPE_ALSA);
    ERRCHECK(result);

    result = system->init(32, FMOD_INIT_NORMAL, 0);
    ERRCHECK(result);

    result = system->createSound("../media/drumloop.wav", FMOD_SOFTWARE, 0, &sound1);
    ERRCHECK(result);
    result = sound1->setMode(FMOD_LOOP_OFF);
    ERRCHECK(result);

    /*
        Main loop.
    */
 int i=0;
result = system->playSound(FMOD_CHANNEL_FREE, sound1, 0, &channel);
        ERRCHECK(result);
   do {
   i=i+1;
 result = system->update();
        ERRCHECK(result);

 } while (i<822000);

    result = sound1->release();
    ERRCHECK(result);
    result = system->close();
    ERRCHECK(result);
    result = system->release();
    ERRCHECK(result);
    return 0;
}
  • You must to post comments
Best Answer
0
0

The call to System::playSound is asynchronous, the code will continue while the audio starts playing on a different thread. It is important to call System::update regularly, generally once per game frame to update the state of the system.

You shouldn’t block your code within the do while loop, allow other parts of your code to execute while the sound is playing.

If you need to know when the sound is finished, used Channel::isPlaying.

  • Michaila Goula-Dimitriou
    I had already created a new thread in my program in which I was planning to call a function to play a sound once. That’s why I want the loop gone. I am not using fmod in game.
  • Mathew Block
    For applications that don’t run a game loop you could put System::update in your GUI message handling code via some timer event or have a separate thread that handles the FMOD calls and update.
  • Michaila Goula-Dimitriou
    Thank you very much. Your answers were really helpful.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.