我想使用 AssetManager 直接从扩展 OBB 文件加载文件。我实现了自己的FileHandleResolver
public class CustomFileHandleResolver implements FileHandleResolver
{
@Override
public FileHandle resolve(String fileName) {
return new CustomFileHandle(fileName);
}
}
我将其设置为我的 AssetManager。我创建了自己的 FileHandle 并重写了 read() 函数
@Override
public InputStream read()
{
InputStream input = null;
try {
input = GameInfo.expansionFile.getInputStream(file.getPath().replace('\\', '/'));
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
它加载所有文件,如 .PNG、.PACK、.FNT,除了 .OGG 文件,所以我猜所有声音文件都不会被加载。我收到此错误:
com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: SFx/button_click.ogg
还有这个错误:
com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ClassCastException: com.solidgamesstudio.threedefendersn.framework.CustomFileHandle cannot be cast to com.badlogic.gdx.backends.android.AndroidFileHandle
我读到zip不能被压缩。在7zip中我选择压缩为“Store”,这样它就根本不被压缩,但仍然出现这个问题。
我遍历了加载文件时发生的情况,发现 AssetManager 调用了我的CustomFileHandleResolver
这创造了CustomFileHandle
。对于每个不是 .OGG 的文件,它调用InputStream read()
。在此函数中,它从 zip 加载文件,这很好。但正如我所说,当加载 .OGG 时,它不会调用此函数。所以它甚至还没有尝试从 zip 中获取文件。问题是,为什么 .OGG 文件不调用InputStream read()
in CustomFileHandle()
?
UPDATE
遍历多了发现不会调用InputStream read()
因为它无法以某种方式从 FileHandle 创建声音。线索是
CustomFileHandle cannot be cast to AndroidFileHandle
要创建声音,您需要传递 fileHandle。
public Sound newSound (FileHandle fileHandle);
这是从调用的SoundLoader
@Override
public void loadAsync (AssetManager manager, String fileName, FileHandle file, SoundParameter parameter) {
sound = Gdx.audio.newSound(file);
}
该 soundLoader 使用我的 CustomFileHandleResolver。我不知道声音的处理方式是否与其他类型的文件不同。但默认情况下 AssetManager 使用
public class InternalFileHandleResolver implements FileHandleResolver {
@Override
public FileHandle resolve (String fileName) {
return Gdx.files.internal(fileName);
}
}
我无法进入Gdx.files.internal
看看是否对声音有任何特殊处理。
UPDATE
进一步的分析给我线索,主要问题是前面提到的。
CustomFileHandle cannot be cast to AndroidFileHandle
我不知道为什么在加载 OGG 文件时将我的 FileHandle 转换为 AndroidFileHandle 。如果它加载其他类型的文件,这可能意味着它不会为它们进行转换。这意味着OGG很特殊,需要选角。有什么线索吗?