From bd757e6232732ea898d76950d426abad448b539f Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 28 Oct 2016 11:30:29 +0800 Subject: [PATCH] Updates lite audio engine sources from upstream (cocos2d-x). --- native/cocos/audio/android/AudioDecoder.cpp | 22 +++--- .../cocos/audio/android/AudioEngine-inl.cpp | 70 ++++++++++++++----- native/cocos/audio/android/AudioEngine-inl.h | 8 ++- .../audio/android/AudioMixerController.cpp | 32 ++------- .../audio/android/AudioMixerController.h | 1 - .../audio/android/AudioPlayerProvider.cpp | 62 ++++++++-------- native/cocos/audio/android/AudioResampler.cpp | 16 ++--- native/cocos/audio/android/CCThreadPool.h | 1 - native/cocos/audio/android/PcmAudioPlayer.cpp | 1 + native/cocos/audio/android/UrlAudioPlayer.cpp | 26 ++++--- .../cocos/audio/android/ccdandroidUtils.cpp | 2 +- native/cocos/audio/android/ccdandroidUtils.h | 2 +- .../audio/android/cddSimpleAudioEngine.cpp | 2 +- .../jni/cddandroidAndroidJavaEngine.cpp | 4 +- .../android/jni/cddandroidAndroidJavaEngine.h | 2 +- native/cocos/audio/android/utils/Utils.cpp | 20 +----- native/cocos/audio/android/utils/Utils.h | 2 +- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 4 ++ 18 files changed, 148 insertions(+), 129 deletions(-) diff --git a/native/cocos/audio/android/AudioDecoder.cpp b/native/cocos/audio/android/AudioDecoder.cpp index 273d1fd8e7..2343c09d41 100644 --- a/native/cocos/audio/android/AudioDecoder.cpp +++ b/native/cocos/audio/android/AudioDecoder.cpp @@ -134,19 +134,19 @@ bool AudioDecoder::start() do { ret = decodeToPcm(); - if (!ret) + if (!ret) { ALOGE("decodeToPcm (%s) failed!", _url.c_str()); break; } ret = resample(); - if (!ret) + if (!ret) { ALOGE("resample (%s) failed!", _url.c_str()); break; } ret = interleave(); - if (!ret) + if (!ret) { ALOGE("interleave (%s) failed!", _url.c_str()); break; @@ -397,20 +397,20 @@ bool AudioDecoder::decodeToPcm() value = nullptr; valueSize = 0; result = (*mdExtrItf)->GetKeySize(mdExtrItf, i, &keySize); - SL_RETURN_VAL_IF_FAILED(result, false, "GetKeySize(%d) failed", i); + SL_RETURN_VAL_IF_FAILED(result, false, "GetKeySize(%d) failed", (int)i); result = (*mdExtrItf)->GetValueSize(mdExtrItf, i, &valueSize); - SL_RETURN_VAL_IF_FAILED(result, false, "GetValueSize(%d) failed", i); + SL_RETURN_VAL_IF_FAILED(result, false, "GetValueSize(%d) failed", (int)i); keyInfo = (SLMetadataInfo *) malloc(keySize); if (nullptr != keyInfo) { result = (*mdExtrItf)->GetKey(mdExtrItf, i, keySize, keyInfo); - SL_RETURN_VAL_IF_FAILED(result, false, "GetKey(%d) failed", i); + SL_RETURN_VAL_IF_FAILED(result, false, "GetKey(%d) failed", (int)i); ALOGV("key[%d] size=%d, name=%s, value size=%d", - i, keyInfo->size, keyInfo->data, valueSize); + (int)i, (int)keyInfo->size, keyInfo->data, (int)valueSize); /* find out the key index of the metadata we're interested in */ if (!strcmp((char *) keyInfo->data, ANDROID_KEY_PCMFORMAT_NUMCHANNELS)) { @@ -477,7 +477,7 @@ bool AudioDecoder::decodeToPcm() _result.pcmBuffer->size() / _result.numChannels / (_result.bitsPerSample / 8); std::string info = _result.toString(); - ALOGI("Original audio info: %s, total size: %d", info.c_str(), _result.pcmBuffer->size()); + ALOGI("Original audio info: %s, total size: %d", info.c_str(), (int)_result.pcmBuffer->size()); return true; } @@ -589,7 +589,7 @@ bool AudioDecoder::resample() (char *) convert + outputFrames * channels * sizeof(int16_t)); _result.pcmBuffer = buffer; - ALOGV("pcm buffer size: %d", _result.pcmBuffer->size()); + ALOGV("pcm buffer size: %d", (int)_result.pcmBuffer->size()); free(convert); free(outputVAddr); @@ -622,7 +622,7 @@ void AudioDecoder::queryAudioInfo() ALOGV("Content duration is unknown (in dec callback)"); } else { - ALOGV("Content duration is %ums (in dec callback)", durationInMsec); + ALOGV("Content duration is %dms (in dec callback)", (int)durationInMsec); _result.duration = durationInMsec / 1000.0f; } @@ -727,7 +727,7 @@ void AudioDecoder::decodeToPcmCallback(SLAndroidSimpleBufferQueueItf queueItf) SLmillisecond msec; result = (*_decContext.playItf)->GetPosition(_decContext.playItf, &msec); SL_RETURN_IF_FAILED(result, "%s, GetPosition failed", __FUNCTION__); - ALOGV("%s called (iteration %d): current position=%u ms", __FUNCTION__, _counter, msec); + ALOGV("%s called (iteration %d): current position=%d ms", __FUNCTION__, _counter, (int)msec); } _result.pcmBuffer->insert(_result.pcmBuffer->end(), _decContext.pData, diff --git a/native/cocos/audio/android/AudioEngine-inl.cpp b/native/cocos/audio/android/AudioEngine-inl.cpp index 49ccfbef61..22a1da2a69 100644 --- a/native/cocos/audio/android/AudioEngine-inl.cpp +++ b/native/cocos/audio/android/AudioEngine-inl.cpp @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014-2015 Chukong Technologies Inc. + Copyright (c) 2014-2016 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -49,7 +49,8 @@ #include "audio/android/IAudioPlayer.h" #include "audio/android/ICallerThreadUtils.h" #include "audio/android/AudioPlayerProvider.h" -#include "audio/android/cutils/log.h" +#include "audio/android/cutils/log.h" +#include "audio/android/UrlAudioPlayer.h" using namespace cocos2d; using namespace cocos2d::experimental; @@ -83,7 +84,7 @@ static CallerThreadUtils __callerThreadUtils; static int fdGetter(const std::string& url, off_t* start, off_t* length) { - int fd = 0; + int fd = -1; if (cocos2d::FileUtilsAndroid::getObbFile() != nullptr) { fd = getObbAssetFileDescriptorJNI(url.c_str(), start, length); @@ -175,19 +176,9 @@ bool AudioEngineImpl::init() _audioPlayerProvider = new AudioPlayerProvider(_engineEngine, _outputMixObject, getDeviceSampleRate(), getDeviceAudioBufferSizeInFrames(), fdGetter, &__callerThreadUtils); - _onPauseListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_COME_TO_BACKGROUND, [this](EventCustom*){ - if (_audioPlayerProvider != nullptr) - { - _audioPlayerProvider->pause(); - } - }); + _onPauseListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_COME_TO_BACKGROUND, CC_CALLBACK_1(AudioEngineImpl::onEnterBackground, this)); - _onResumeListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_COME_TO_FOREGROUND, [this](EventCustom*){ - if (_audioPlayerProvider != nullptr) - { - _audioPlayerProvider->resume(); - } - }); + _onResumeListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(AudioEngineImpl::onEnterForeground, this)); ret = true; }while (false); @@ -195,6 +186,45 @@ bool AudioEngineImpl::init() return ret; } +void AudioEngineImpl::onEnterBackground(EventCustom* event) +{ + // _audioPlayerProvider->pause() pauses AudioMixer and PcmAudioService, + // but UrlAudioPlayers could not be paused. + if (_audioPlayerProvider != nullptr) + { + _audioPlayerProvider->pause(); + } + + // pause UrlAudioPlayers which are playing. + for (auto&& e : _audioPlayers) + { + auto player = e.second; + if (dynamic_cast(player) != nullptr + && player->getState() == IAudioPlayer::State::PLAYING) + { + _urlAudioPlayersNeedResume.push_back(player); + player->pause(); + } + } +} + +void AudioEngineImpl::onEnterForeground(EventCustom* event) +{ + // _audioPlayerProvider->resume() resumes AudioMixer and PcmAudioService, + // but UrlAudioPlayers could not be resumed. + if (_audioPlayerProvider != nullptr) + { + _audioPlayerProvider->resume(); + } + + // resume UrlAudioPlayers + for (auto&& player : _urlAudioPlayersNeedResume) + { + player->resume(); + } + _urlAudioPlayersNeedResume.clear(); +} + int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume) { ALOGV("play2d, _audioPlayers.size=%d", (int)_audioPlayers.size()); @@ -219,17 +249,21 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if (state != IAudioPlayer::State::OVER && state != IAudioPlayer::State::STOPPED) { - ALOGV("Ignore state: %d", state); + ALOGV("Ignore state: %d", static_cast(state)); return; } int id = player->getId(); - ALOGV("Removing player id=%d", id); + ALOGV("Removing player id=%d, state:%d", id, (int)state); + auto iter = _callbackMap.find(id); if (iter != _callbackMap.end()) { - iter->second(id, *AudioEngine::_audioIDInfoMap[id].filePath); + if (state == IAudioPlayer::State::OVER) + { + iter->second(id, *AudioEngine::_audioIDInfoMap[id].filePath); + } _callbackMap.erase(iter); } AudioEngine::remove(id); diff --git a/native/cocos/audio/android/AudioEngine-inl.h b/native/cocos/audio/android/AudioEngine-inl.h index 0038086c76..620f1fbe30 100644 --- a/native/cocos/audio/android/AudioEngine-inl.h +++ b/native/cocos/audio/android/AudioEngine-inl.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014-2015 Chukong Technologies Inc. + Copyright (c) 2014-2016 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -74,6 +74,9 @@ public: private: + void onEnterBackground(EventCustom* event); + void onEnterForeground(EventCustom* event); + // engine interfaces SLObjectItf _engineObject; SLEngineItf _engineEngine; @@ -85,6 +88,9 @@ private: std::unordered_map _audioPlayers; std::unordered_map> _callbackMap; + // UrlAudioPlayers which need to resumed while entering foreground + std::vector _urlAudioPlayersNeedResume; + AudioPlayerProvider* _audioPlayerProvider; EventListener* _onPauseListener; EventListener* _onResumeListener; diff --git a/native/cocos/audio/android/AudioMixerController.cpp b/native/cocos/audio/android/AudioMixerController.cpp index 534f01a1f8..1e46af338d 100644 --- a/native/cocos/audio/android/AudioMixerController.cpp +++ b/native/cocos/audio/android/AudioMixerController.cpp @@ -98,6 +98,9 @@ static void removeItemFromVector(std::vector& v, T item) void AudioMixerController::initTrack(Track* track, std::vector& tracksToRemove) { + if (track->isInitialized()) + return; + uint32_t channelMask = audio_channel_out_mask_from_count(2); int32_t name = _mixer->getTrackName(channelMask, AUDIO_FORMAT_PCM_16_BIT, AUDIO_SESSION_OUTPUT_MIX); @@ -133,7 +136,6 @@ void AudioMixerController::initTrack(Track* track, std::vector& tracksTo AudioMixer::CHANNEL_MASK, (void *) (uintptr_t) channelMask); - track->setState(Track::State::PLAYING); track->setName(name); _mixer->enable(name); @@ -192,16 +194,9 @@ void AudioMixerController::mixOneFrame() { state = track->getState(); - if (state == Track::State::IDLE) + if (state == Track::State::PLAYING) { initTrack(track, tracksToRemove); - } - else if (state == Track::State::PLAYING) - { - if (!track->isInitialized()) - { - initTrack(track, tracksToRemove); - } int name = track->getName(); ALOG_ASSERT(name >= 0); @@ -224,10 +219,7 @@ void AudioMixerController::mixOneFrame() } else if (state == Track::State::RESUMED) { - if (!track->isInitialized()) - { - initTrack(track, tracksToRemove); - } + initTrack(track, tracksToRemove); if (track->getPrevState() == Track::State::PAUSED) { @@ -241,10 +233,7 @@ void AudioMixerController::mixOneFrame() } else if (state == Track::State::PAUSED) { - if (!track->isInitialized()) - { - initTrack(track, tracksToRemove); - } + initTrack(track, tracksToRemove); if (track->getPrevState() == Track::State::PLAYING || track->getPrevState() == Track::State::RESUMED) { @@ -259,14 +248,7 @@ void AudioMixerController::mixOneFrame() { if (track->isInitialized()) { - if (track->getPrevState() != Track::State::IDLE) - { - _mixer->deleteTrackName(track->getName()); - } - else - { - ALOGV("Stop track (%p) while it's in IDLE state!", track); - } + _mixer->deleteTrackName(track->getName()); } else { diff --git a/native/cocos/audio/android/AudioMixerController.h b/native/cocos/audio/android/AudioMixerController.h index c3f7b7c9ff..bb4915641b 100644 --- a/native/cocos/audio/android/AudioMixerController.h +++ b/native/cocos/audio/android/AudioMixerController.h @@ -54,7 +54,6 @@ public: bool init(); bool addTrack(Track* track); - bool hasPlayingTacks(); void pause(); diff --git a/native/cocos/audio/android/AudioPlayerProvider.cpp b/native/cocos/audio/android/AudioPlayerProvider.cpp index 0cff40786a..8727eb209e 100644 --- a/native/cocos/audio/android/AudioPlayerProvider.cpp +++ b/native/cocos/audio/android/AudioPlayerProvider.cpp @@ -48,7 +48,7 @@ static int getSystemAPILevel() return __systemApiLevel; } - int apiLevel = getSystemProperty("ro.build.version.sdk"); + int apiLevel = getSDKVersion(); if (apiLevel > 0) { ALOGD("Android API level: %d", apiLevel); @@ -210,16 +210,16 @@ void AudioPlayerProvider::preloadEffect(const std::string &audioFilePath, const return; } + _pcmCacheMutex.lock(); + auto&& iter = _pcmCache.find(audioFilePath); + if (iter != _pcmCache.end()) { - std::lock_guard lk(_pcmCacheMutex); - auto&& iter = _pcmCache.find(audioFilePath); - if (iter != _pcmCache.end()) - { - ALOGV("preload return from cache: (%s)", audioFilePath.c_str()); - cb(true, iter->second); - return; - } + ALOGV("preload return from cache: (%s)", audioFilePath.c_str()); + _pcmCacheMutex.unlock(); + cb(true, iter->second); + return; } + _pcmCacheMutex.unlock(); auto info = getFileInfo(audioFilePath); preloadEffect(info, [this, cb, audioFilePath](bool succeed, PcmData data){ @@ -246,17 +246,17 @@ void AudioPlayerProvider::preloadEffect(const AudioFileInfo &info, const Preload { std::string audioFilePath = info.url; + // 1. First time check, if it wasn't in the cache, goto 2 step + _pcmCacheMutex.lock(); + auto&& iter = _pcmCache.find(audioFilePath); + if (iter != _pcmCache.end()) { - // 1. First time check, if it wasn't in the cache, goto 2 step - std::lock_guard lk(_pcmCacheMutex); - auto&& iter = _pcmCache.find(audioFilePath); - if (iter != _pcmCache.end()) - { - ALOGV("1. Return pcm data from cache, url: %s", info.url.c_str()); - cb(true, iter->second); - return; - } + ALOGV("1. Return pcm data from cache, url: %s", info.url.c_str()); + _pcmCacheMutex.unlock(); + cb(true, iter->second); + return; } + _pcmCacheMutex.unlock(); { // 2. Check whether the audio file is being preloaded, if it has been removed from map just now, @@ -273,17 +273,19 @@ void AudioPlayerProvider::preloadEffect(const AudioFileInfo &info, const Preload return; } - { // 3. Check it in cache again. If it has been removed from map just now, the file is in - // the cache absolutely. - std::lock_guard lk2(_pcmCacheMutex); - auto&& iter = _pcmCache.find(audioFilePath); - if (iter != _pcmCache.end()) - { - ALOGV("2. Return pcm data from cache, url: %s", info.url.c_str()); - cb(true, iter->second); - return; - } + // 3. Check it in cache again. If it has been removed from map just now, the file is in + // the cache absolutely. + _pcmCacheMutex.lock(); + auto&& iter = _pcmCache.find(audioFilePath); + if (iter != _pcmCache.end()) + { + ALOGV("2. Return pcm data from cache, url: %s", info.url.c_str()); + _pcmCacheMutex.unlock(); + cb(true, iter->second); + return; } + _pcmCacheMutex.unlock(); + PreloadCallbackParam param; param.callback = cb; @@ -342,7 +344,7 @@ AudioPlayerProvider::AudioFileInfo AudioPlayerProvider::getFileInfo( AudioFileInfo info; long fileSize = 0; off_t start = 0, length = 0; - int assetFd = 0; + int assetFd = -1; if (audioFilePath[0] != '/') { @@ -469,7 +471,7 @@ UrlAudioPlayer *AudioPlayerProvider::createUrlAudioPlayer( return nullptr; } - SLuint32 locatorType = info.assetFd > 0 ? SL_DATALOCATOR_ANDROIDFD : SL_DATALOCATOR_URI; + SLuint32 locatorType = info.assetFd->getFd() > 0 ? SL_DATALOCATOR_ANDROIDFD : SL_DATALOCATOR_URI; auto urlPlayer = new (std::nothrow) UrlAudioPlayer(_engineItf, _outputMixObject, _callerThreadUtils); bool ret = urlPlayer->prepare(info.url, locatorType, info.assetFd, info.start, info.length); if (!ret) diff --git a/native/cocos/audio/android/AudioResampler.cpp b/native/cocos/audio/android/AudioResampler.cpp index d8b26396a3..ab12289360 100644 --- a/native/cocos/audio/android/AudioResampler.cpp +++ b/native/cocos/audio/android/AudioResampler.cpp @@ -107,14 +107,14 @@ static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUAL void AudioResampler::init_routine() { - int resamplerQuality = getSystemProperty("af.resampler.quality"); - if (resamplerQuality > 0) { - defaultQuality = (src_quality) resamplerQuality; - ALOGD("forcing AudioResampler quality to %d", defaultQuality); - if (defaultQuality < DEFAULT_QUALITY || defaultQuality > VERY_HIGH_QUALITY) { - defaultQuality = DEFAULT_QUALITY; - } - } + // int resamplerQuality = getSystemProperty("af.resampler.quality"); + // if (resamplerQuality > 0) { + // defaultQuality = (src_quality) resamplerQuality; + // ALOGD("forcing AudioResampler quality to %d", defaultQuality); + // if (defaultQuality < DEFAULT_QUALITY || defaultQuality > VERY_HIGH_QUALITY) { + // defaultQuality = DEFAULT_QUALITY; + // } + // } } uint32_t AudioResampler::qualityMHz(src_quality quality) diff --git a/native/cocos/audio/android/CCThreadPool.h b/native/cocos/audio/android/CCThreadPool.h index f4f6f02639..143e1c8791 100644 --- a/native/cocos/audio/android/CCThreadPool.h +++ b/native/cocos/audio/android/CCThreadPool.h @@ -36,7 +36,6 @@ #include #include #include -#include namespace cocos2d { namespace experimental { diff --git a/native/cocos/audio/android/PcmAudioPlayer.cpp b/native/cocos/audio/android/PcmAudioPlayer.cpp index 8bd8f126d4..dd39707a60 100644 --- a/native/cocos/audio/android/PcmAudioPlayer.cpp +++ b/native/cocos/audio/android/PcmAudioPlayer.cpp @@ -145,6 +145,7 @@ void PcmAudioPlayer::play() // put track to AudioMixerController ALOGV("PcmAudioPlayer (%p) play (%s) ...", this, _url.c_str()); _controller->addTrack(_track); + _track->setState(Track::State::PLAYING); } void PcmAudioPlayer::pause() diff --git a/native/cocos/audio/android/UrlAudioPlayer.cpp b/native/cocos/audio/android/UrlAudioPlayer.cpp index cb1b74a02c..54bbafc1f1 100644 --- a/native/cocos/audio/android/UrlAudioPlayer.cpp +++ b/native/cocos/audio/android/UrlAudioPlayer.cpp @@ -169,7 +169,7 @@ void UrlAudioPlayer::stop() } else { - ALOGW("UrlAudioPlayer (%p, state:%d) isn't playing or paused, could not invoke stop!", this, _state); + ALOGW("UrlAudioPlayer (%p, state:%d) isn't playing or paused, could not invoke stop!", this, static_cast(_state)); } } @@ -183,7 +183,7 @@ void UrlAudioPlayer::pause() } else { - ALOGW("UrlAudioPlayer (%p, state:%d) isn't playing, could not invoke pause!", this, _state); + ALOGW("UrlAudioPlayer (%p, state:%d) isn't playing, could not invoke pause!", this, static_cast(_state)); } } @@ -197,7 +197,7 @@ void UrlAudioPlayer::resume() } else { - ALOGW("UrlAudioPlayer (%p, state:%d) isn't paused, could not invoke resume!", this, _state); + ALOGW("UrlAudioPlayer (%p, state:%d) isn't paused, could not invoke resume!", this, static_cast(_state)); } } @@ -211,7 +211,7 @@ void UrlAudioPlayer::play() } else { - ALOGW("UrlAudioPlayer (%p, state:%d) isn't paused or initialized, could not invoke play!", this, _state); + ALOGW("UrlAudioPlayer (%p, state:%d) isn't paused or initialized, could not invoke play!", this, static_cast(_state)); } } @@ -276,7 +276,18 @@ bool UrlAudioPlayer::prepare(const std::string &url, SLuint32 locatorType, std:: _url = url; _assetFd = assetFd; - ALOGV("UrlAudioPlayer::prepare: %s, %d, %d, %d, %d", _url.c_str(), (int)locatorType, assetFd->getFd(), start, + const char* locatorTypeStr= "UNKNOWN"; + if (locatorType == SL_DATALOCATOR_ANDROIDFD) + locatorTypeStr = "SL_DATALOCATOR_ANDROIDFD"; + else if (locatorType == SL_DATALOCATOR_URI) + locatorTypeStr = "SL_DATALOCATOR_URI"; + else + { + ALOGE("Oops, invalid locatorType: %d", (int)locatorType); + return false; + } + + ALOGV("UrlAudioPlayer::prepare: %s, %s, %d, %d, %d", _url.c_str(), locatorTypeStr, _assetFd->getFd(), start, length); SLDataSource audioSrc; @@ -303,11 +314,6 @@ bool UrlAudioPlayer::prepare(const std::string &url, SLuint32 locatorType, std:: audioSrc.pLocator = &locUri; ALOGV("locUri: locatorType: %d", (int)locUri.locatorType); } - else - { - ALOGE("Oops, invalid locatorType: %d", (int)locatorType); - return false; - } // configure audio sink SLDataLocator_OutputMix locOutmix = {SL_DATALOCATOR_OUTPUTMIX, _outputMixObj}; diff --git a/native/cocos/audio/android/ccdandroidUtils.cpp b/native/cocos/audio/android/ccdandroidUtils.cpp index f948b74a8c..e01ad51586 100644 --- a/native/cocos/audio/android/ccdandroidUtils.cpp +++ b/native/cocos/audio/android/ccdandroidUtils.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2014 Chukong Technologies Inc. +Copyright (c) 2013-2016 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/native/cocos/audio/android/ccdandroidUtils.h b/native/cocos/audio/android/ccdandroidUtils.h index 0b80088e1a..79106952f4 100644 --- a/native/cocos/audio/android/ccdandroidUtils.h +++ b/native/cocos/audio/android/ccdandroidUtils.h @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2014 Chukong Technologies Inc. +Copyright (c) 2013-2016 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/native/cocos/audio/android/cddSimpleAudioEngine.cpp b/native/cocos/audio/android/cddSimpleAudioEngine.cpp index c029d12ad3..169338099f 100644 --- a/native/cocos/audio/android/cddSimpleAudioEngine.cpp +++ b/native/cocos/audio/android/cddSimpleAudioEngine.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2014 Chukong Technologies Inc. +Copyright (c) 2013-2016 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp b/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp index 7639129e14..6333f629ce 100644 --- a/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp +++ b/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2015 Chukong Technologies Inc. +Copyright (c) 2013-2016 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -48,7 +48,7 @@ AndroidJavaEngine::AndroidJavaEngine() : _implementBaseOnAudioEngine(false) , _effectVolume(1.f) { - int sdkVer = getSystemProperty("ro.build.version.sdk"); + int sdkVer = getSDKVersion(); if (sdkVer > 0) { __android_log_print(ANDROID_LOG_DEBUG, "cocos2d", "android SDK version:%d", sdkVer); diff --git a/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.h b/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.h index 5f578d23db..8329002595 100644 --- a/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.h +++ b/native/cocos/audio/android/jni/cddandroidAndroidJavaEngine.h @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2015 Chukong Technologies Inc. +Copyright (c) 2013-2016 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/native/cocos/audio/android/utils/Utils.cpp b/native/cocos/audio/android/utils/Utils.cpp index 4fccb8e942..6f7dc34a2d 100644 --- a/native/cocos/audio/android/utils/Utils.cpp +++ b/native/cocos/audio/android/utils/Utils.cpp @@ -22,27 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "audio/android/utils/Utils.h" - -#include -#include +#include "platform/android/jni/JniHelper.h" namespace cocos2d { namespace experimental { -int getSystemProperty(const std::string& property) +int getSDKVersion() { - int ret = -1; - std::string command = "getprop " + property; - FILE* file = popen(command.c_str(), "r"); - if (file) - { - char output[100]; - if (std::fgets(output, sizeof(output), file) != nullptr) - ret = std::atoi(output); - - pclose(file); - } - - return ret; + return JniHelper::callStaticIntMethod("org/cocos2dx/lib/Cocos2dxHelper", "getSDKVersion"); } }} // end of namespace cocos2d { namespace experimental diff --git a/native/cocos/audio/android/utils/Utils.h b/native/cocos/audio/android/utils/Utils.h index 34c8211fcd..d4d2415321 100644 --- a/native/cocos/audio/android/utils/Utils.h +++ b/native/cocos/audio/android/utils/Utils.h @@ -25,5 +25,5 @@ THE SOFTWARE. #include namespace cocos2d { namespace experimental { - extern int getSystemProperty(const std::string& property); + extern int getSDKVersion(); }} diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index c201d645b3..c53b269014 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -663,4 +663,8 @@ public class Cocos2dxHelper { public static float[] getCompassValue() { return Cocos2dxHelper.sCocos2dxAccelerometer.compassFieldValues; } + + public static int getSDKVersion() { + return Build.VERSION.SDK_INT; + } }