实现方式:
- 这个方案没有采用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 官方库
打包完毕文件后缀名 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文件管理器
- 挂载为可读写
- 上传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/