我希望能够通过使用AssetManager直接从扩展OBB文件中加载文件。我实现了自己的
它加载所有像 .PNG、.PACK、.FNT 这样的文件,但不包括 .OGG 文件,因此我猜测所有音频文件都无法被加载。 我收到了以下错误信息:
这是一个错误提示信息:
我看到zip文件无法压缩。 我在7zip中选择了“存储”压缩,这样就完全没有被压缩,但仍然出现此问题。
我浏览了文件加载时发生的情况,发现AssetManager调用我的CustomFileHandleResolver,它创建CustomFileHandle。对于每个不是.OGG的文件,它调用InputStream read()。在此函数中,它从zip文件中加载文件,一切正常。但是如我所说,当它要加载.OGG时,它不会调用此函数。所以它甚至还没有尝试从zip中获取文件。问题是,为什么.OGG文件不在CustomFileHandle()中调用InputStream read()?
更新
我更详细地查看后发现它无法从FileHandle创建声音。 这表明无法从OGG文件中创建声音。
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中选择了“存储”压缩,这样就完全没有被压缩,但仍然出现此问题。
我浏览了文件加载时发生的情况,发现AssetManager调用我的CustomFileHandleResolver,它创建CustomFileHandle。对于每个不是.OGG的文件,它调用InputStream read()。在此函数中,它从zip文件中加载文件,一切正常。但是如我所说,当它要加载.OGG时,它不会调用此函数。所以它甚至还没有尝试从zip中获取文件。问题是,为什么.OGG文件不在CustomFileHandle()中调用InputStream read()?
更新
我更详细地查看后发现它无法从FileHandle创建声音。 这表明无法从OGG文件中创建声音。
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);
}
我使用了自定义的文件处理器CustomFileHandleResolver作为soundLoader。我不知道声音文件是否与其他文件类型有所不同,但默认情况下,AssetManager会使用
public class InternalFileHandleResolver implements FileHandleResolver {
@Override
public FileHandle resolve (String fileName) {
return Gdx.files.internal(fileName);
}
}
我无法访问Gdx.files.internal
以查看是否有任何特殊处理声音的方法。
更新:
进一步分析使我得出结论,正如之前所提到的那样,主要问题在于这个问题。
CustomFileHandle cannot be cast to AndroidFileHandle
我不知道为什么在加载OGG文件时它会把我的FileHandle转换成AndroidFileHandle。如果它可以正常加载其他类型的文件,那可能意味着它不会对它们进行强制类型转换。这意味着OGG是特殊的,并且需要进行类型转换。有任何线索吗?
FileHandleResolver
?你只需要执行assetManager.load("SFx/button_click.ogg", Sound.class)
和Sound sound = assetManager.get("SFx/button_click.ogg", Sound.class)
。这是libgdx的所有标准功能。 - noone