This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
また、停止した音を再開する処理を盛り込んでいません。ここは、ゲーム毎の実装依存が多いと思いあえてしませんでした。例えば多くの場合は再開すべき音はBGMだけであったりするでしょう。
AndroidのOpenSL ESはコールバックは別スレッドから来るので特に気をつける必要があります。Android特有の実装についてはNDKのdocs/Additional_library_docs/opensles/index.html に説明があるので、一度目を通しておくと良さそうです。
No comments:
Post a Comment