diff --git a/native/cocos/audio/AudioEngine.cpp b/native/cocos/audio/AudioEngine.cpp index ed74cf317e..2fc5a5ab1b 100644 --- a/native/cocos/audio/AudioEngine.cpp +++ b/native/cocos/audio/AudioEngine.cpp @@ -362,6 +362,12 @@ void AudioEngine::onEnterBackground(const CustomEvent &event) { _breakAudioID.push_back(it->first); } } + +#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID + if (_audioEngineImpl) { + _audioEngineImpl->onPause(); + } +#endif } void AudioEngine::onEnterForeground(const CustomEvent &event) { @@ -370,6 +376,12 @@ void AudioEngine::onEnterForeground(const CustomEvent &event) { _audioEngineImpl->resume(*it); } _breakAudioID.clear(); + +#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID + if (_audioEngineImpl) { + _audioEngineImpl->onResume(); + } +#endif } void AudioEngine::stop(int audioID) diff --git a/native/cocos/audio/android/AudioEngine-inl.cpp b/native/cocos/audio/android/AudioEngine-inl.cpp index e7a6ed9784..d9c713a1ad 100644 --- a/native/cocos/audio/android/AudioEngine-inl.cpp +++ b/native/cocos/audio/android/AudioEngine-inl.cpp @@ -429,4 +429,20 @@ void AudioEngineImpl::uncacheAll() } } +void AudioEngineImpl::onPause() +{ + if (_audioPlayerProvider != nullptr) + { + _audioPlayerProvider->pause(); + } +} + +void AudioEngineImpl::onResume() +{ + if (_audioPlayerProvider != nullptr) + { + _audioPlayerProvider->resume(); + } +} + #endif diff --git a/native/cocos/audio/android/AudioEngine-inl.h b/native/cocos/audio/android/AudioEngine-inl.h index be6ddf0a3f..68a0650b61 100644 --- a/native/cocos/audio/android/AudioEngine-inl.h +++ b/native/cocos/audio/android/AudioEngine-inl.h @@ -73,6 +73,9 @@ public: void uncacheAll(); void preload(const std::string& filePath, const std::function& callback); + void onResume(); + void onPause(); + void setAudioFocusForAllPlayers(bool isFocus); private: // engine interfaces diff --git a/native/cocos/audio/apple/AudioEngine-inl.mm b/native/cocos/audio/apple/AudioEngine-inl.mm index 5763fb2c77..fdf47fb360 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.mm +++ b/native/cocos/audio/apple/AudioEngine-inl.mm @@ -687,9 +687,14 @@ void AudioEngineImpl::update(float dt) _threadMutex.lock(); it = _audioPlayers.erase(it); _threadMutex.unlock(); - - if (player->_finishCallbak) { - player->_finishCallbak(audioID, filePath); //IDEA: callback will delay 50ms + + if(auto sche = _scheduler.lock()) { + if (player->_finishCallbak) { + auto cb = player->_finishCallbak; + sche->performFunctionInCocosThread([audioID, cb, filePath](){ + cb(audioID, filePath); //IDEA: callback will delay 50ms + }); + } } delete player; diff --git a/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp b/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp index 300b0ec7ea..2cdc9cf7ed 100644 --- a/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp +++ b/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp @@ -294,7 +294,7 @@ namespace se { thiz->_isErrorHandleWorking = true; Value errorHandler; - if (thiz->_globalObj->getProperty("__errorHandler", &errorHandler) && errorHandler.isObject() && errorHandler.toObject()->isFunction()) + if (thiz->_globalObj && thiz->_globalObj->getProperty("__errorHandler", &errorHandler) && errorHandler.isObject() && errorHandler.toObject()->isFunction()) { ValueArray args; args.push_back(resouceNameVal); diff --git a/native/extensions/assets-manager/AssetsManagerEx.cpp b/native/extensions/assets-manager/AssetsManagerEx.cpp index 71e5756b33..bbb0fdf539 100644 --- a/native/extensions/assets-manager/AssetsManagerEx.cpp +++ b/native/extensions/assets-manager/AssetsManagerEx.cpp @@ -699,20 +699,8 @@ void AssetsManagerEx::parseVersion() } else { - _updateState = State::NEED_UPDATE; - - // Wait to update so continue the process - if (_updateEntry == UpdateEntry::DO_UPDATE) - { - // dispatch after checking update entry because event dispatching may modify the update entry - dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND); - _updateState = State::PREDOWNLOAD_MANIFEST; - downloadManifest(); - } - else - { - dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND); - } + _updateState = State::PREDOWNLOAD_MANIFEST; + downloadManifest(); } } } @@ -763,12 +751,17 @@ void AssetsManagerEx::parseManifest() else { _updateState = State::NEED_UPDATE; - dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND); - + if (_updateEntry == UpdateEntry::DO_UPDATE) { startUpdate(); } + else if (_updateEntry == UpdateEntry::CHECK_UPDATE) + { + prepareUpdate(); + } + + dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND); } } } @@ -795,6 +788,16 @@ void AssetsManagerEx::prepareUpdate() _tempManifest->genResumeAssetsList(&_downloadUnits); _totalWaitToDownload = _totalToDownload = (int)_downloadUnits.size(); _downloadResumed = true; + + // Collect total size + for(auto iter : _downloadUnits) + { + const DownloadUnit& unit = iter.second; + if (unit.size > 0) + { + _totalSize += unit.size; + } + } } else { @@ -828,21 +831,17 @@ void AssetsManagerEx::prepareUpdate() for (auto it = diff_map.begin(); it != diff_map.end(); ++it) { Manifest::AssetDiff diff = it->second; - if (diff.type == Manifest::DiffType::DELETED) - { - std::string exsitedPath = _storagePath + diff.asset.path; - _fileUtils->removeFile(exsitedPath); - } - else + if (diff.type != Manifest::DiffType::DELETED) { std::string path = diff.asset.path; DownloadUnit unit; unit.customId = it->first; - unit.srcUrl = packageUrl + path; + unit.srcUrl = packageUrl + path + "?md5=" + diff.asset.md5; unit.storagePath = _tempStoragePath + path; unit.size = diff.asset.size; _downloadUnits.emplace(unit.customId, unit); _tempManifest->setAssetDownloadState(it->first, Manifest::DownloadState::UNSTARTED); + _totalSize += unit.size; } } // Start updating the temp manifest @@ -864,6 +863,7 @@ void AssetsManagerEx::startUpdate() } if (_updateState == State::READY_TO_UPDATE) { + _totalSize = 0; _updateState = State::UPDATING; std::string msg; if (_downloadResumed) @@ -882,12 +882,20 @@ void AssetsManagerEx::startUpdate() void AssetsManagerEx::updateSucceed() { // Set temp manifest's updating - _tempManifest->setUpdating(false); + if (_tempManifest != nullptr) { + _tempManifest->setUpdating(false); + } // Every thing is correctly downloaded, do the following // 1. rename temporary manifest to valid manifest - _fileUtils->renameFile(_tempStoragePath, TEMP_MANIFEST_FILENAME, MANIFEST_FILENAME); - // 2. merge temporary storage path to storage path so that temporary version turns to cached version + if (_fileUtils->isFileExist(_tempManifestPath)) { + _fileUtils->renameFile(_tempStoragePath, TEMP_MANIFEST_FILENAME, MANIFEST_FILENAME); + } + + // 2. Get the delete files + std::unordered_map diff_map = _localManifest->genDiff(_remoteManifest); + + // 3. merge temporary storage path to storage path so that temporary version turns to cached version if (_fileUtils->isDirectoryExist(_tempStoragePath)) { // Merging all files in temp storage path to storage path @@ -913,21 +921,40 @@ void AssetsManagerEx::updateSucceed() } _fileUtils->renameFile(*it, dstPath); } + + // Remove from delete list for safe, although this is not the case in general. + auto diff_itr = diff_map.find(relativePath); + if (diff_itr != diff_map.end()) { + diff_map.erase(diff_itr); + } + } + + // Preprocessing local files in previous version and creating download folders + for (auto it = diff_map.begin(); it != diff_map.end(); ++it) + { + Manifest::AssetDiff diff = it->second; + if (diff.type == Manifest::DiffType::DELETED) + { + // TODO: Do this when download finish, it don’t matter delete or not. + std::string exsitedPath = _storagePath + diff.asset.path; + _fileUtils->removeFile(exsitedPath); + } } - // Remove temp storage path - _fileUtils->removeDirectory(_tempStoragePath); } - // 3. swap the localManifest + + // 4. swap the localManifest CC_SAFE_RELEASE(_localManifest); _localManifest = _remoteManifest; _localManifest->setManifestRoot(_storagePath); _remoteManifest = nullptr; - // 4. make local manifest take effect + // 5. make local manifest take effect prepareLocalManifest(); - // 5. Set update state + // 6. Set update state _updateState = State::UP_TO_DATE; - // 6. Notify finished event + // 7. Notify finished event dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_FINISHED); + // 8. Remove temp storage path + _fileUtils->removeDirectory(_tempStoragePath); } void AssetsManagerEx::checkUpdate() @@ -1024,6 +1051,7 @@ void AssetsManagerEx::update() } break; case State::FAIL_TO_UPDATE: + case State::READY_TO_UPDATE: case State::NEED_UPDATE: { // Manifest not loaded yet @@ -1032,7 +1060,7 @@ void AssetsManagerEx::update() _updateState = State::PREDOWNLOAD_MANIFEST; downloadManifest(); } - else + else if (_updateEntry == UpdateEntry::DO_UPDATE) { startUpdate(); } @@ -1319,12 +1347,12 @@ void AssetsManagerEx::queueDowload() void AssetsManagerEx::onDownloadUnitsFinished() { + // Always save current download manifest information for resuming + _tempManifest->saveToFile(_tempManifestPath); + // Finished with error check if (_failedUnits.size() > 0) { - // Save current download manifest information for resuming - _tempManifest->saveToFile(_tempManifestPath); - _updateState = State::FAIL_TO_UPDATE; dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_FAILED); }