LoginSignup
2

More than 5 years have passed since last update.

[cocos2d-x]v3.8でAudioEngineのpreloadは動くようになった?

Posted at

概要

cocos2dで音楽を扱う場合、標準ではSimpleAudioEngine、あるいはその後発のAudioEngineを使うことになると思います。
以前はSimpleAudioEngineにはpreloadがあるのにAudioEngineにはpreloadが無かったそうですが、v3.8辺りでpreloadが対応したとのこと。

しかしこのpreload、どうやらAndroidでは動いていないようで。
※cocos2d-x v3.10で調べました。

ソースを見てみる

iOSの場合、preloadの実装を辿ると以下のような実装にたどり着きます

AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function<void(bool)> callback)
{
    AudioCache* audioCache = nullptr;

    auto it = _audioCaches.find(filePath);
    if (it == _audioCaches.end()) {
        audioCache = &_audioCaches[filePath];
        audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);

        AudioEngine::addTask(std::bind(&AudioCache::readDataTask, audioCache));
    }
    else {
        audioCache = &it->second;
    }

    if(audioCache && callback)
    {
        audioCache->addLoadCallback(callback);
    }
    return audioCache;
}

iOSは大丈夫そう。
androidの方からたどり着く先は…

void AudioEngineImpl::preload(const std::string& filePath, std::function<void(bool)> callback)
{
    CCLOG("Preload not support on Anroid");
    if (callback)
    {
        callback(false);
    }
}
Preload not support on Anroid

(´・ω・`)

Android側はopenSL/ESで実装されているそうですが、preloadやcacheはできないのですかね。
android4.1以降latencyはかなり改善されているそうですが…
自前で書く場合どのようにしたらいいんだろう。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2