The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
audio callback does not stop after playing sound
slash


Joined: 04 Jun 2014
Posts: 5
Hi,

I just tried to play a sound file using SDL_OpenAudio. I used sample code from a tutorial:

http://www.spieleprogrammierer.de/27-tutorials/7210-simple-directmedia-layer-sdl-tutorial/

and only replaced the sound file and used memset() instead of ZeroMemory in my implementation.

When I try to play the sound, playing does not end when the soundfile is processed fully.

I modified my callback function to monitor the variables:

Code:

// Callbackfunktion
void audio_msg(void *pReserved, Uint8* stream, int len)
{
    fprintf(stderr, "len=%d\n", len);
    fprintf(stderr, "audio_pos=%d\n", audio_pos);
    fprintf(stderr, "audio_len=%d\n", audio_len);
    //
    // Wenn kein Sound mehr, beenden
    if (audio_len == 0) {
      fprintf(stderr, "*****audio_len=%d\n", audio_len);
        return;
    }

    // Mixen
    len = ( len > audio_len ? audio_len : len );
    fprintf(stderr, "len2=%d\n", len);
    SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    audio_pos += len;
    audio_len -= len;
}


The sound is played continously until the following SDL_Delay() is finished:

Code:

len=1024
audio_pos=153604904
audio_len=2936
len2=1024
len=1024
audio_pos=153605928
audio_len=1912
len2=1024
len=1024
audio_pos=153606952
audio_len=888
len2=888
len=1024
audio_pos=153607840
audio_len=0
*****audio_len=0
len=1024
audio_pos=153607840
audio_len=0
*****audio_len=0
len=1024


I would have expected, that playing ends when audio_len reaches 0. Am I wrong? If so, what would be the correct way to play a sound file?

I'm using SDL2 on o Ubuntu 12.04 box.

Thanks in advance for any hint.

Cheers,

slash