SDL: how to stop audio playback - not to pause it?

Hi SDL,

I am developing iOS application which plays samples - it does not play them
in loop, it just has to play one sample once, and later on, play another
sample, again just once.
But I am in trouble, since the only way to play/stop audio I know about, is
to call SDL_PauseAudio(0)/SDL_PauseAudio(1). Yep, I am using callback
mechanism to play my samples.

My problem is that SDL wants to play my samples in loop. Hence, as soon as
my code detects that entire sampleA has been played, it calls
SDL_PauseAudio(1) to ‘stop’ playing. When it calls SDL_PauseAudio(0) to
play play another sample, sampleB, the loop is resumed. But SDL plays also
some fragment from sampleA, then it goes to play sampleB.

I had to edit SDL_CoreAudio.c to ‘fix’ this problem:

Index: src/audio/coreaudio/SDL_coreaudio.c===================================================================
— src/audio/coreaudio/SDL_coreaudio.c
+++ src/audio/coreaudio/SDL_coreaudio.c
@@ -250,6 +250,12 @@
abuf = &ioData->mBuffers[i];
SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
}

  • if (2 == this->paused)
  • {
  • // this changes ‘pause’ behavior to ‘stop’ behavior - next
  • // plyaing starts from beginning, i.e. it won’t resume
  • this->hidden->bufferOffset = this->hidden->bufferSize;
  • }
    return 0;
    }

I have not found nothing in SDL API which could solve my issue, hence I did
the change. Does it really mean, that SDL cannot play audio other way then
in infinite loop? There’s really nobody who would like SDL to play a sample
from memory just once?

I see all this in the mixer:
/* Halt playing of a particular channel /
extern DECLSPEC int SDLCALL Mix_HaltChannel(int channel);
extern DECLSPEC int SDLCALL Mix_HaltGroup(int tag);
extern DECLSPEC int SDLCALL Mix_HaltMusic(void);
/
Change the expiration delay for a particular channel.
The sample will stop playing after the ‘ticks’ milliseconds have elapsed,
or remove the expiration if ‘ticks’ is -1
/
extern DECLSPEC int SDLCALL Mix_ExpireChannel(int channel, int ticks);
/
Halt a channel, fading it out progressively till it’s silent
The ms parameter indicates the number of milliseconds the fading
will take.
/
extern DECLSPEC int SDLCALL Mix_FadeOutChannel(int which, int ms);
extern DECLSPEC int SDLCALL Mix_FadeOutGroup(int tag, int ms);
extern DECLSPEC int SDLCALL Mix_FadeOutMusic(int ms);
/
Query the fading status of a channel /
extern DECLSPEC Mix_Fading SDLCALL Mix_FadingMusic(void);
extern DECLSPEC Mix_Fading SDLCALL Mix_FadingChannel(int which);
/
Pause/Resume a particular channel /
extern DECLSPEC void SDLCALL Mix_Pause(int channel);
extern DECLSPEC void SDLCALL Mix_Resume(int channel);
extern DECLSPEC int SDLCALL Mix_Paused(int channel);
/
Pause/Resume the music stream /
extern DECLSPEC void SDLCALL Mix_PauseMusic(void);
extern DECLSPEC void SDLCALL Mix_ResumeMusic(void);
extern DECLSPEC void SDLCALL Mix_RewindMusic(void);
extern DECLSPEC int SDLCALL Mix_PausedMusic(void);
/
Stop music and set external music playback command */
extern DECLSPEC int SDLCALL Mix_SetMusicCMD(const char command);
/
Synchro value is set by MikMod from modules while playing /
extern DECLSPEC int SDLCALL Mix_SetSynchroValue(int value);
extern DECLSPEC int SDLCALL Mix_GetSynchroValue(void);
/
Close the mixer, halting all playing audio */
extern DECLSPEC void SDLCALL Mix_CloseAudio(void);
I guess that last one is a hail mary that will work no matter what. Have
you taken a look here and tried these things?On Wed, Nov 16, 2011 at 8:26 AM, Pete Lefis wrote:

Hi SDL,

I am developing iOS application which plays samples - it does not play
them in loop, it just has to play one sample once, and later on, play
another sample, again just once.
But I am in trouble, since the only way to play/stop audio I know about,
is to call SDL_PauseAudio(0)/SDL_PauseAudio(1). Yep, I am using callback
mechanism to play my samples.

My problem is that SDL wants to play my samples in loop. Hence, as soon as
my code detects that entire sampleA has been played, it calls
SDL_PauseAudio(1) to ‘stop’ playing. When it calls SDL_PauseAudio(0) to
play play another sample, sampleB, the loop is resumed. But SDL plays also
some fragment from sampleA, then it goes to play sampleB.

I had to edit SDL_CoreAudio.c to ‘fix’ this problem:

Index: src/audio/coreaudio/SDL_coreaudio.c

— src/audio/coreaudio/SDL_coreaudio.c
+++ src/audio/coreaudio/SDL_coreaudio.c
@@ -250,6 +250,12 @@
abuf = &ioData->mBuffers[i];
SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
}

  • if (2 == this->paused)
  • {
  • // this changes ‘pause’ behavior to ‘stop’ behavior - next
  • // plyaing starts from beginning, i.e. it won’t resume
  • this->hidden->bufferOffset = this->hidden->bufferSize;
  • }
    return 0;
    }

I have not found nothing in SDL API which could solve my issue, hence I
did the change. Does it really mean, that SDL cannot play audio other way
then in infinite loop? There’s really nobody who would like SDL to play a
sample from memory just once?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org