实现方式:

这个方案没有采用google 的jobb 方式打包obb而是通过zip包的方式进行打包obb

在android部分获取Sound, Music,Video 时是通过 AssetFileDescriptor 进行读取的所以 通过设置 android 中的反射 addAssetPath 进行添加路径 下面代码会有用到注意查看

cocos 方面修改部分c++ 代码让cocos能够读取到obb包中的内容

obb包直接通过winrar 进行打包 这里注意 压缩时选择zip格式,压缩方式选择->存储

5.https://pan.baidu.com/s/1i507k5f zip读取类 google 官方库

obb打包方式

打包完毕文件后缀名 zip 修改obb (obb 命名方式 mian.versionCode.packageName.obb 注意:assets目录为根目录)

以下为代码实现 不要直接复制只是把需要修改的地方进行说明 根据自己情况进行修改

当前使用版本为2.x

3.x版本的应该大同小异自己修改下对应的地方就行了

1.获取obb包路径在项目的 MainActivity.java(这个文件可能名字不一样 继承于 Cocos2dxActivity)

public static String FATE_OBB_PATH= "";

public void onCreate(Bundle savedInstanceState) {

//获取obb 路径

FATE_OBB_PATH =getVirtualObbFileFullpath() ;//这句需要放在super.onCreate上面

super.onCreate(savedInstanceState);

...

}

public String getObbFileName() {

PackageInfo info = null;

try {

info = super.getPackageManager().getPackageInfo(super.getPackageName(), 0);

String fileName = "main." + info.versionCode + "." + getPackageName() + ".obb";

return fileName;

} catch (PackageManager.NameNotFoundException e) {

e.printStackTrace();

}

return "";

}

public String getVirtualObbFileFullpath(){

File sdcardDir = Environment.getExternalStorageDirectory();

String _path = getObbDir().getPath() + "/" + getObbFileName();

Log.e("===_path===", _path);

return _path;

}

2. Cocos2dxHelper 代码修改

public static ZipResourceFile obbzip = null;

public static void init(final Context pContext, final Cocos2dxHelperListener pCocos2dxHelperListener) {

...

// begin--------------------添加代码----------------------------

//检查obb文件是否存在

if(fileIsExists(MainActivity.FATE_OBB_PATH)){

//存在添加obb路径到cocos中 注意 nativeSetObbPath 方法是需要新添加的 下方会介绍

Cocos2dxHelper.nativeSetObbPath(MainActivity.FATE_OBB_PATH);

}

// end--------------------添加代码----------------------------

Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir);

Cocos2dxHelper.sCocos2dxAccelerometer = new Cocos2dxAccelerometer(pContext);

Cocos2dxHelper.sCocos2dMusic = new Cocos2dxMusic(pContext);

int simultaneousStreams = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_DEFAULT;

if (Cocos2dxHelper.getDeviceModel().indexOf("GT-I9100") != -1) {

simultaneousStreams = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_I9100;

}

Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(pContext, simultaneousStreams);

Cocos2dxHelper.sAssetManager = pContext.getAssets();

//设置压缩包

PackageInfo info = null;

try {

info = pContext.getPackageManager().getPackageInfo(pContext.getPackageName(), 0);

Cocos2dxHelper.obbzip = APKExpansionSupport.getAPKExpansionZipFile(pContext,info.versionCode,0);

} catch (PackageManager.NameNotFoundException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

// end--------------------添加代码----------------------------

PSNetwork.init(pContext);

Cocos2dxBitmap.setContext(pContext);

Cocos2dxETCLoader.setContext(pContext);

}

//检查obb文件是否存在

public static boolean fileIsExists(String strFile)

{

try

{

File f=new File(strFile);

if(!f.exists())

{

return false;

}

}

catch (Exception e)

{

return false;

}

return true;

}

private static native void nativeSetApkPath(final String pApkPath);

//nativeSetObbPath 设置obb路径方法

private static native void nativeSetObbPath(final String pObbPath);

3. 修改 Cocos2dxMusic.java 和 Cocos2dxSound.java 将获得AssetFileDescriptor的地方加入obb包查找

//Cocos2dxMusic.java

final AssetFileDescriptor assetFileDescritor = Cocos2dxHelper.obbzip.getAssetFileDescriptor("assets/"+pPath);

if(assetFileDescritor == null) {

final AssetFileDescriptor assetFileDescritor1 = this.mContext.getAssets().openFd(pPath);

mediaPlayer.setDataSource(assetFileDescritor1.getFileDescriptor(), assetFileDescritor1.getStartOffset(), assetFileDescritor1.getLength());

}else{

mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());

}

//Cocos2dxSound.java

final AssetFileDescriptor assetFileDescritor = Cocos2dxHelper.obbzip.getAssetFileDescriptor("assets/"+pPath);

if(assetFileDescritor == null) {

final AssetFileDescriptor assetFileDescritor1 = this.mContext.getAssets().openFd(pPath);

soundID = this.mSoundPool.load(assetFileDescritor1, 0);

}else{

soundID = this.mSoundPool.load(assetFileDescritor, 0);

}

4. 修改 Java_org_cocos2dx_lib_Cocos2dxHelper.cpp

string g_apkPath;

//添加obb path

string g_obbPath;

//添加设置obbpath 方法

JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetObbPath(JNIEnv* env, jobject thiz, jstring obbPath) {

g_obbPath = JniHelper::jstring2string(obbPath);

}

//添加获取obbpath 方法

const char * getObbPath() {

return g_obbPath.c_str();

}

5.修改 CCFileUtilsAndroid.h

//添加方法

extern const char * getObbPath();

6.修改 CCFileUtilsAndroid.cpp

static ZipFile *s_pZipFile = NULL;

//在 s_pZipFile 下添加一个 obb zip包解析

static ZipFile *s_pZipFileobb = NULL;

//在sharedFileUtils() 中创建obb的zip

CCFileUtils* CCFileUtils::sharedFileUtils()

{

if (s_sharedFileUtils == NULL)

{

s_sharedFileUtils = new CCFileUtilsAndroid();

s_sharedFileUtils->init();

std::string resourcePath = getApkPath();

s_pZipFile = new ZipFile(resourcePath, "assets/");

// begin ------------------代码添加

//获取obb路径

std::string resourcePath_Obb = getObbPath();

// 创建obbzip

s_pZipFileobb = new ZipFile(resourcePath_Obb,"assets/");

// end --------------------代码添加

}

return s_sharedFileUtils;

}

CCFileUtilsAndroid::~CCFileUtilsAndroid()

{

CC_SAFE_DELETE(s_pZipFile);

//销毁

CC_SAFE_DELETE(s_pZipFileobb);

}

//文件检查中的修改

bool CCFileUtilsAndroid::isFileExist(const std::string& strFilePath)

{

if (0 == strFilePath.length())

{

return false;

}

bool bFound = false;

// Check whether file exists in apk.

if (strFilePath[0] != '/')

{

std::string strPath = strFilePath;

if (strPath.find(m_strDefaultResRootPath) != 0)

{// Didn't find "assets/" at the beginning of the path, adding it.

strPath.insert(0, m_strDefaultResRootPath);

}

if (s_pZipFile->fileExists(strPath))

{

bFound = true;

}

// begin -------------代码添加

if(!bFound){

if (s_pZipFileobb->fileExists(strPath))

{

bFound = true;

}

}

// end -----------------代码添加

}

else

{

FILE *fp = fopen(strFilePath.c_str(), "r");

if(fp)

{

bFound = true;

fclose(fp);

}

}

return bFound;

}

//文件获取方法中的代码修改

unsigned char* CCFileUtilsAndroid::doGetFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize, bool forAsync)

{

unsigned char * pData = 0;

if ((! pszFileName) || (! pszMode) || 0 == strlen(pszFileName))

{

return 0;

}

string fullPath = fullPathForFilename(pszFileName);

if (fullPath[0] != '/')

{

if (forAsync)

{

pData = s_pZipFile->getFileData(fullPath.c_str(), pSize, s_pZipFile->_dataThread);

// begin -------------代码添加

if (! pData)

{

pData = s_pZipFileobb->getFileData(fullPath.c_str(), pSize, s_pZipFile->_dataThread);

}

//end -------------代码添加

}

else

{

pData = s_pZipFile->getFileData(fullPath.c_str(), pSize);

// begin -------------代码添加

if (! pData)

{

pData = s_pZipFileobb->getFileData(fullPath.c_str(), pSize);

}

//end -------------代码添加

}

}

else

{

do

{

// read rrom other path than user set it

//CCLOG("GETTING FILE ABSOLUTE DATA: %s", pszFileName);

FILE *fp = fopen(fullPath.c_str(), pszMode);

CC_BREAK_IF(!fp);

unsigned long size;

fseek(fp,0,SEEK_END);

size = ftell(fp);

fseek(fp,0,SEEK_SET);

pData = new unsigned char[size];

size = fread(pData,sizeof(unsigned char), size,fp);

fclose(fp);

if (pSize)

{

*pSize = size;

}

} while (0);

}

if (! pData)

{

std::string msg = "Get data from file(";

msg.append(pszFileName).append(") failed!");

CCLOG("%s", msg.c_str());

}

return pData;

}

测试:

Root手机

安装 re文件管理器

re文件管理器

挂载为可读写

挂载为可读写

上传obb包

检查是否有 对应目录 /storage/emulated/0/Android/obb/com.k0204.game/ 如果没有自己创建一下

adb push E:\main.5.com.k0204.game.obb /storage/emulated/0/Android/obb/com.k0204.game/

cocos2dx android obb,cocos2dx 实现obb包读取 quick2.2.6相关推荐

  1. cocos2dx 3.0 Google使用obb扩展包

    cocos2dx 3.16 已经支持使用Google obb扩展包 首先项目支持Google play service AndroidManifest.xml相关权限和工程配置 <uses-pe ...

  2. cocos2dx Android接入芒果插屏广告

    cocos2dx Android接入芒果插屏广告 时间 2014-12-06 18:39:19  CSDN博客原文  http://blog.csdn.net/techkuki/article/det ...

  3. android obb在哪,obb是什么文件 obb文件怎么用

    android小白新手在下载游戏的时候可能都会提示下载数据包,其实.obb文件就是所谓的游戏数据包,只不过有的obb文件被压缩成了压缩文件.数据包的使用直接影响游戏的进程.下载数据包之后,可能还会提示 ...

  4. android obb在哪,.obb是什么文件?obb文件怎么用/放在哪里

    android小白新手在下载游戏的时候可能都会提示下载数据包,其实.obb文件就是所谓的游戏数据包,只不过有的obb文件被压缩成了压缩文件.数据包的使用直接影响游戏的进程.下载数据包之后,可能还会提示 ...

  5. 安卓手机Android文件夹下obb文件是什么,obb是什么文件?怎么使用obb文件夹

    类型:休闲益智大小:46.4M语言:英文 评分:5.0 标签: 立即下载 obb文件就是所谓的游戏数据包,只不过有的obb文件被压缩成了压缩文件.数据包的使用直接影响游戏的进程.下载数据包之后,可能还 ...

  6. Cocos2d-x + Android + Eclipse + Windows 8

    琢磨着弄弄Cocos2d-x,配置下 Cocos2d-x + Android + Eclipse + Windows 8 的开发环境,过程比较心酸曲折,做个记录以备后用 我的基础环境 不做详述,不清楚 ...

  7. [Android开发]cocos2dx工程中接入支付宝sdk

    cocos2dx工程中接入支付宝sdk 1. 首先去支付宝官网下载开发者文档 2. 然后按着开发者文档将支付宝的sdk导入到你的工程中,并关联到工程中,步骤入下图: (1)将从支付宝官方网站获得的支付 ...

  8. 自制工具:Cocos2d-x Android.mk文件自动修改器

    做cocos2d-x的项目,一般是用电脑进行开发,然后移植到手机平台上.移植到安卓手机需要用eclipse等工具重新编译打包成apk文件.而用eclipse打包的话,要把项目的cpp文件一条条加入到A ...

  9. Android 10开发之 保存、读取图片

    Android 10开发之 保存.读取图片 概述 从Android 10(Q)开始,谷歌就开始修改了外部存储权限,叫做分区存储,分区存储可以分为两个目录,分别是 沙盒目录(App-specific d ...

最新文章

  1. CentOS 6.7 RPM安装MySQL
  2. 计算机使用DHCP动态分配参数,某单位采用DHCP进行IP地址自动分配,用户收到()消息后方可使用其中分配的IP - 信管网...
  3. windows常见的运行命令以及各快捷键组合
  4. oracle创建表空间 扩展表空间文件 修改表空间自动增长
  5. PDH光端机的作用及其特点
  6. 单链表的应用 就地逆置
  7. 2019年中国IaaS公有云市场排名及份额出炉
  8. 【excel技巧读书笔记015】同时关闭多张工作薄
  9. myfunc matlab,为matlab匿名函数设置’help’
  10. Http协议对格式、请求头、方法
  11. java翻译smali_【翻译】apk反汇编之smali语法
  12. Linux修改open files数及ulimit和file-max的区别
  13. 计算字符串占用字节数
  14. ANSYS License管理器程序的步骤-Windows版
  15. 支付宝对账单CSV解析
  16. Windows远程应用发布
  17. 数据结构排序之“九阳神功”
  18. 9本4月程序员新书,Python书就占了6本
  19. C++ for循环效率优化
  20. PrintDocument打印、预览、打印机设置和打印属性的方法(较完整)

热门文章

  1. labview中前面板如何设置背景图片
  2. excel链接隐藏工作表_在Excel 2007和2010中隐藏和取消隐藏工作表和工作簿
  3. 统计学 假设检验 P值
  4. r语言中的多因素方差分析_R中的因素
  5. 51单片机课设代做_微波炉控制系统设计
  6. 运维工程师到底是个啥?
  7. app混合开发之微信分享设置
  8. 电力,地铁,医生等行业值班员全能倒班日历助手
  9. H5、React Native、Native应用对比分析
  10. 个人永久性免费-Excel催化剂功能第86波-人工智能之图像OCR文本识别全覆盖