Skip to content

Instantly share code, notes, and snippets.

@yorung
Last active December 20, 2015 15:51
Show Gist options
  • Select an option

  • Save yorung/fee4c48f6856ee9c7035 to your computer and use it in GitHub Desktop.

Select an option

Save yorung/fee4c48f6856ee9c7035 to your computer and use it in GitHub Desktop.
void Voice::Play(bool loop)
{
if (!IsReady()) {
return;
}
auto playback = [](SLAndroidSimpleBufferQueueItf q, void* context_) {
double now = GetTime();
if (now - systemMisc.GetLastUpdateTime() >= 0.5) {
return;
}
WaveContext* context = (WaveContext*)context_;
int totalSize;
const void* buf = RiffFindChunk(context->fileImg, "data", &totalSize);
int enqueued = context->enqueuedSize;
if (enqueued >= totalSize) {
if (!context->loop) {
return;
}
enqueued = 0;
}
int toEnqueue = std::min(totalSize - enqueued, 32768);
enqueued += toEnqueue;
context->enqueuedSize = enqueued;
SLCall(q, Enqueue, (char*)buf + enqueued - toEnqueue, toEnqueue);
};
SLCall(context->playerPlay, SetPlayState, SL_PLAYSTATE_STOPPED);
SLCall(context->playerBufferQueue, RegisterCallback, playback, context);
context->enqueuedSize = 0;
context->loop = loop;
SLCall(context->playerPlay, SetPlayState, SL_PLAYSTATE_PLAYING);
playback(context->playerBufferQueue, context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment