0
0

Hy

I have a simple problem.

Im playing an stream ( a mp3 file) with fmod. ( My code for playing a stream is equal to the fmod examples). I’ve made a method playmp3() which init. fmod and plays the stream.
How is ist now possible to close this stream while it is playing and play another mp3 file. I always get a messsage like : Segmentation failed.

Thank u

Rain

  • You must to post comments
0
0

Thank u for your help:

Here is my code:
[code:1owmo9v8]
// INCLUDES..............
using namespace std;
int channel = -1;
FSOUND_STREAM *stream;
FSOUND_SAMPLE *sptr;
char key;

signed char endcallback(FSOUND_STREAM *stream, void *buff, int len, int param)
{

if (buff)
{
    printf("\nSYNCHPOINT : \"%s\"\n", buff);
}
else
{
    printf("\nSTREAM ENDED!!\n");
}

return TRUE;

}

void playtrack()
{

if 0

else

stream = FSOUND_Stream_OpenFile("test.mp3", FSOUND_NORMAL | FSOUND_MPEGACCURATE | FSOUND_LOOP_OFF, 0);
if (!stream)
{
    printf("Error!\n");
    printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));
    FSOUND_Close();

 }

endif

FSOUND_Stream_SetEndCallback(stream, endcallback, 0);
FSOUND_Stream_SetSynchCallback(stream, endcallback, 0);
sptr = FSOUND_Stream_GetSample(stream);
if (sptr)
{
    int freq;
    FSOUND_Sample_GetDefaults(sptr, &freq, NULL, NULL, NULL);

}

key = 0;
do
{
    if (channel < 0)
    {
        // ======================
        // PLAY STREAM
        // ======================
        channel = FSOUND_Stream_PlayEx(FSOUND_FREE, stream, NULL, TRUE);
        FSOUND_SetPaused(channel, FALSE);
    }

    if (kbhit())
    {
        key = getch();

    if(key == 's')
    {
    FSOUND_Stream_Close(stream);
    FSOUND_Sample_Free(sptr);
    stream =NULL;
    sptr = NULL;
    playtrack();
    }

}

Sleep(10);

}while (key != 27);

FSOUND_Stream_Close(stream);

FSOUND_Close();

}

void initfsound()
{
if (!FSOUND_Init(44100, 32, 0))
{
printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));
FSOUND_Close();

}

}

int main(int argc, char *argv[])
{

ifdef _WIN32

FSOUND_SetOutput(FSOUND_OUTPUT_WINMM);

elif defined(linux)

FSOUND_SetOutput(FSOUND_OUTPUT_OSS);

endif

// ==========================================================================================
// SELECT DRIVER
// ==========================================================================================
{
    long i,driver=0;
    char key;

    // The following list are the drivers for the output method selected above.
    printf("---------------------------------------------------------\n");
    switch (FSOUND_GetOutput())
    {
        case FSOUND_OUTPUT_NOSOUND:    printf("NoSound"); break;
        case FSOUND_OUTPUT_WINMM:      printf("Windows Multimedia Waveout"); break;
        case FSOUND_OUTPUT_DSOUND:     printf("Direct Sound"); break;
        case FSOUND_OUTPUT_A3D:        printf("A3D"); break;
        case FSOUND_OUTPUT_OSS:        printf("Open Sound System"); break;
        case FSOUND_OUTPUT_ESD:        printf("Enlightenment Sound Daemon"); break;
        case FSOUND_OUTPUT_ALSA:       printf("ALSA"); break;
    };
    printf(" Driver list\n");
    printf("---------------------------------------------------------\n");

    for (i=0; i < FSOUND_GetNumDrivers(); i++)
    {
        printf("%d - %s\n", i+1, FSOUND_GetDriverName(i));    // print driver names
    }
    printf("---------------------------------------------------------\n");    // print driver names
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        key = getch();
        if (key == 27) exit(0);

        driver = key - '1';
    } while (driver < 0 || driver >= FSOUND_GetNumDrivers());

    FSOUND_SetDriver(driver);                    // Select sound card (0 = default)
}

// =================
// INITIALIZE

initfsound();
playtrack();
return 0;

}
[/code:1owmo9v8]

In the main methde i call fsoundinit() and the playtrack() and the file is played by fmod. This works. But if you press while the file is playing the key ‘s’ playtrack() is called and the programm doesn’t work anymore. -> Segmentation fault.
I took many of this code from the fmod examples. I want to stop a mp3 while it is played and then to start playing a other mp3. I use the linux version of fmod.

please excuse my bad grammar
Thank u

Rain

  • You must to post comments
0
0

I’m not 100% sure if this will fix your problem, but it looks like the reason the stream doesn’t reset itself when you press the ‘s’ key is that you only call FSOUND_Stream_PlayEx() if channel is less than zero. You initialize it to -1 at the beginning of the program, so the MP3 plays the first time. But when you stop the song and call playtrack() again, channel still has its old (positive non-zero) value, and the stream doesn’t get played again. You want to be setting channel to zero after closing the stream and freeing sptr.

As for the segmentation fault, I [i:3n169wf0]suspect[/i:3n169wf0] it might have something to do with infinite recursion…each time you call playtrack(), you get stuck in a do-while loop waiting for the value of key to be 27. When ‘s’ is pressed, playtrack() calls itself, and you end up in an identical do-while loop, one level deeper. Anyway, I don’t know the specifics of your kbhit() function, but I’m guessing that something isn’t being reset correctly, and so kbhit() is always returning true. That way, when you press ‘s’, playtrack() calls itself, which calls itself again and again and again until you blow your stack and cause a segmentation fault. To see if this is happening, try putting a printf(“entering playtrack\n”) statement at the beginning of playtrack().

If this doesn’t fix the problem, could you use a debugger and tell us exactly where the segmentation fault is occuring?

  • You must to post comments
0
0

Thank u.

I’ve made a few changes in my code and now it works.

Rain

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.