参考范例合集,我使用Downloader下载一个较大的文件,真机上但是几乎100%闪退。而我换成使用AssetsManagerEx热更的方式去下载同一个文件,却能够顺利下载完成。可是我翻看AssetsManagerEx.cpp的代码,发现里面使用的也是Downloader呀,折腾了好几天,百思不得其姐~
以下是我写的demo,改自helloworld项目,真机上几乎必闪退。使用的真机是700块的手机三星J3-109和1000多的华为荣耀8都闪退,(换成AssetsManagerEx则正常)
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!',
startBtn: cc.Node,
},
// use this for initialization
onLoad: function () {
this.label.string = this.text;
// 该zip文件,大小约为40M。
this.dataUrl = 'http://192.168.2.189:8001/remote-assets/tanchuang.zip'
this._downloader = new jsb.Downloader();
this._downloader.setOnFileTaskSuccess(this.onSucceed.bind(this));
this._downloader.setOnTaskProgress(this.onProgress.bind(this));
this._downloader.setOnTaskError(this.onError.bind(this));
},
btnStartDownload: function () {
this._downloader.createDownloadFileTask(this.dataUrl, jsb.fileUtils.getWritablePath() + 'tanchuang.zip');
this.label.string = 'start downloading ...'
this.startBtn.active = false
},
onSucceed (task) {
this.label.string = 'finish'
},
onProgress (task, bytesReceived, totalBytesReceived, totalBytesExpected) {
this.label.string = `${bytesReceived} ${totalBytesReceived} ${totalBytesExpected}`
},
onError (task, errorCode, errorCodeInternal, errorStr) {
this.label.string = 'Failed to download file (' + task.requestURL + '): ' + errorStr + '(' + errorCode + ')';
},
});
以下是使用Android Studio的调试log:
03-27 15:48:46.527 20250-20314/org.cocos2d.myDownloadTest E/cocos2d-x: cocos2d: warning, Director::setProjection() failed because size is 0
03-27 15:48:46.557 20250-20250/org.cocos2d.myDownloadTest D/FLQG_LOG: CustomDebug: CustomResult result : 80000 msg : CUSTOMRESULT_CUSTOMREXTENSION
03-27 15:48:46.597 20250-20250/org.cocos2d.myDownloadTest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@340c9be4 time:305556989
03-27 15:48:46.607 20250-20314/org.cocos2d.myDownloadTest D/cocos2d-x: cocos2d: fullPathForFilename: No file found at script/jsb_prepare.jsc. Possible missing file.
03-27 15:48:46.727 20250-20314/org.cocos2d.myDownloadTest D/cocos2d-x: cocos2d: fullPathForFilename: No file found at script/jsb_boot.jsc. Possible missing file.
03-27 15:48:46.727 20250-20314/org.cocos2d.myDownloadTest D/cocos2d: android SDK version:22
03-27 15:48:47.007 20250-20250/org.cocos2d.myDownloadTest D/Cocos2dxActivity: onPause()
03-27 15:48:47.007 20250-20250/org.cocos2d.myDownloadTest D/AudioFocusManager: abandonAudioFocus succeed!
03-27 15:48:47.047 20250-20250/org.cocos2d.myDownloadTest V/ActivityThread: updateVisibility : ActivityRecord{3fc6c3ac token=android.os.BinderProxy@340c9be4 {org.cocos2d.myDownloadTest/org.cocos2dx.javascript.AppActivity}} show : true
03-27 15:48:47.137 20250-20314/org.cocos2d.myDownloadTest D/cocos2d-x debug info: Cocos2d-x-lite v1.5.2
03-27 15:48:47.647 20250-20314/org.cocos2d.myDownloadTest D/cocos2d-x: create rendererRecreatedListener for GLProgramState
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest D/FLQG_LOG: Statistics: onActionResult(UserDebug,0,init success)
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest W/FLQG_LOG: UserObject: callback of plugin UserDebug not set correctly
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest D/FLQG_LOG: Statistics: onActionResult(CustomDebug,80000,CUSTOMRESULT_CUSTOMREXTENSION)
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest W/FLQG_LOG: CustomObject: Listener of plugin CustomDebug not set correctly
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest D/FLQG_LOG: Statistics: onActionResult(RECDebug,0,init success)
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest W/FLQG_LOG: RECObject: Listener of plugin RECDebug not set correctly
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest D/cocos2d-x: cocos_audioengine_focus_change: 0
03-27 15:48:47.697 20250-20314/org.cocos2d.myDownloadTest W/cocos2d-x: cocos_audioengine_focus_change: AudioEngineImpl isn’t ready!
03-27 15:48:48.357 20250-20250/org.cocos2d.myDownloadTest D/Cocos2dxActivity: onResume()
03-27 15:48:48.357 20250-20250/org.cocos2d.myDownloadTest D/AudioFocusManager: requestAudioFocus succeed
03-27 15:48:48.387 20250-20314/org.cocos2d.myDownloadTest D/cocos2d-x debug info: Success to load scene: db://assets/Scene/helloworld.fire
03-27 15:48:48.487 20250-20250/org.cocos2d.myDownloadTest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@340c9be4 time:305558889
03-27 15:49:08.507 20250-20250/org.cocos2d.myDownloadTest D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
03-27 15:49:24.127 20250-20265/org.cocos2d.myDownloadTest I/art: Background sticky concurrent mark sweep GC freed 319(15KB) AllocSpace objects, 21(4MB) LOS objects, 0% free, 11MB/11MB, paused 1.180ms total 123.202ms
03-27 15:49:27.447 20250-20265/org.cocos2d.myDownloadTest W/art: Suspending all threads took: 9.117ms
03-27 15:49:27.447 20250-20265/org.cocos2d.myDownloadTest I/art: Background partial concurrent mark sweep GC freed 186(7KB) AllocSpace objects, 10(2MB) LOS objects, 24% free, 6MB/9MB, paused 21.784ms total 39.171ms
03-27 15:49:27.887 20250-20265/org.cocos2d.myDownloadTest W/art: Suspending all threads took: 34.868ms
03-27 15:49:27.907 20250-20265/org.cocos2d.myDownloadTest I/art: Background sticky concurrent mark sweep GC freed 356(17KB) AllocSpace objects, 24(4MB) LOS objects, 0% free, 12MB/12MB, paused 46.267ms total 166.743ms
03-27 15:49:30.067 20250-20265/org.cocos2d.myDownloadTest I/art: Background partial concurrent mark sweep GC freed 165(8KB) AllocSpace objects, 15(3MB) LOS objects, 24% free, 6MB/8MB, paused 6.280ms total 18.406ms
03-27 15:49:30.737 20250-20265/org.cocos2d.myDownloadTest I/art: Background sticky concurrent mark sweep GC freed 66(3KB) AllocSpace objects, 5(1041KB) LOS objects, 13% free, 7MB/8MB, paused 81.455ms total 87.958ms
附件是测试的demo项目:
MyDownloaderTest.zip (255.4 KB)