Java – Unable to load sound file LibGdx

Unable to load sound file LibGdx… here is a solution to the problem.

Unable to load sound file LibGdx

I want to use AssetManager to load files directly from the extension OBB file. I implemented my own FileHandleResolver

public class CustomFileHandleResolver implements FileHandleResolver
{
    @Override
    public FileHandle resolve(String fileName) {
        return new CustomFileHandle(fileName);
    }
}

I set it up as my AssetManager. I created my own FileHandle and overridden the read() function

@Override
public InputStream read()
{
    InputStream input = null;

try {           
        input = GameInfo.expansionFile.getInputStream(file.getPath().replace('\\', '/'));
    } catch (IOException e) {
        e.printStackTrace();
    }   

return input;
}

It loads all files such as .PNG, . PACK、. FNT, except for . OGG file, so I guess not all sound files are loaded. I’m getting this error:

com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: SFx/button_click.ogg

And this error :

com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ClassCastException: com.solidgamesstudio.threedefendersn.framework.CustomFileHandle cannot be cast to com.badlogic.gdx.backends.android.AndroidFileHandle

I read that zip cannot be compressed. In 7zip I chose to compress to “Store” so it wasn’t compressed at all, but the issue still occurs.

I

walked through what happened when loading the file, and I found that AssetManager called my CustomFileHandleResolver, which created the CustomFileHandle. For each that is not . OGG file, which calls InputStream read(). In this function, it loads the file from zip and is fine. But as I was loading . OGG does not call this function, as it says. So it hasn’t even tried to get the files from the zip yet. The question is, why . OGG file does not call InputStream read() in CustomFileHandle()?

Update

I traversed more and I found that it doesn’t call InputStream read() because it can’t somehow create a Sound from FileHandle. The clue is

CustomFileHandle cannot be cast to AndroidFileHandle

When

creating a sound, you need to pass a fileHandle.

public Sound newSound (FileHandle fileHandle);

This is called from SoundLoader

@Override
public void loadAsync (AssetManager manager, String fileName, FileHandle file, SoundParameter parameter) {
    sound = Gdx.audio.newSound(file);
}

That soundLoader uses my CustomFileHandleResolver. I don’t know if sound is handled differently than other types of files. But by default AssetManager uses

public class InternalFileHandleResolver implements FileHandleResolver {
    @Override
    public FileHandle resolve (String fileName) {
        return Gdx.files.internal(fileName);
    }
}

I can’t get into Gdx.files.internal to see if there’s any special handling for the sound.

Update

Further analysis gives me clues, the main problem is as mentioned earlier.

CustomFileHandle cannot be cast to AndroidFileHandle

I don’t know why it converts my FileHandle to AndroidFileHandle when loading OGG files. If it loads other types of files well, that probably means it won’t convert for them. This means that OGG is special and requires type conversion. Any clues?

Solution

I haven’t found a way to load sound files from zip files. The problem is that AssetManager loads sound files differently than other file types. The problem is that it is converting FileHandle to AndroidFileHandle, and since CustomFileHandle extends FileHandle, it is not possible to convert it to Android file handle. I can’t find a solution to this problem because it’s deeply rooted.

CustomFileHandle cannot be cast to AndroidFileHandle

In this case, I had to take out all the sound files from the OBB file and put them together with the application. I created another instance of AssetManager to load the sound. So the sound loads as well as with AssetManager, and for any other type of file I use, AssetManager uses my own FileHandlerResolver with my own FileHandle class, which returns the file from the zip. The only problem with this method is that you can only have sound files up to 50 MB in size.

Related Problems and Solutions