无法加载声音文件LibGdx声音文件、加载、LibGdx

2023-09-05 08:40:11 作者:先生眼里没我

我想从扩张OBB文件使用AssetManager直接加载文件。我实现了我自己的 FileHandleResolver

I would like to load files directly from expansion OBB file by using AssetManager. I implemented my own FileHandleResolver

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

我将其设置为我AssetManager。我创建了自己的FileHandle和我重写read()函数

I set it to my AssetManager. I created my own FileHandle and I override read() function

@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文件中的所有文件,所以我想,所有的声音文件就不会被加载。我得到这个错误:

It loads all the files like .PNG, .PACK, .FNT, except .OGG files, so I guess that all sound files won't be 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

和这个错误:

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

我读了拉链不能COM pressed。在7zip的我选择的COM pression存储,因此,它不是COM pressed所有,但仍然出现此问题。

I read that zip can not be compressed. In 7zip I selected compression to "Store" so that it's not compressed at all, but still this problem occurs.

我走过的时候被载入的文件所发生的事情,我发现AssetManager叫我 CustomFileHandleResolver 创造 CustomFileHandle 。对于每次调用未.OGG文件的InputStream的read()。在这个函数加载从zip文件,它的罚款。但正如我所说的,当涉及到装载.OGG它不会调用这个函数。因此,它甚至没有试图尚未得到从zip文件。问题是,为什么.OGG文件不叫的InputStream的read() CustomFileHandle()

I traversed what is happening when files are being loaded and I found that AssetManager calls my CustomFileHandleResolver which creates CustomFileHandle. For every file that is not .OGG it calls InputStream read(). In this function it loads the file from the zip and it's fine. But as I said when it comes to loading .OGG it doesn't call this function. So it's not even trying yet to get the file from the zip. Question is, why .OGG file doesn't call InputStream read() in CustomFileHandle()?

更新

我走过更多,我发现,它不会叫的InputStream的read(),因为它无法创建文件句柄的声音不知。线索,这是

I traversed more and I found out that it won't call InputStream read() because it can't create a Sound from FileHandle somehow. Clue to this is

CustomFileHandle cannot be cast to AndroidFileHandle

在创建你需要传递文件句柄声音。

While to create a sound you need to pass 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用途

And that soundLoader uses my CustomFileHandleResolver. I don't know if Sounds are handled differently then 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);
    }
}

我无法进入 Gdx.files.internal 来看看是否有任何特殊处理的声音。

I can't get into Gdx.files.internal to see if there are any special handling for Sounds.

更新

进一步分析给我的线索,主要的问题是,这是前面提到的。

Further analysis give me clue that the main problem is this as mentioned before.

CustomFileHandle cannot be cast to AndroidFileHandle

我不知道为什么它的铸造我的文件句柄AndroidFileHandle而载入OGG文件。如果它加载罚款等类型的文件,这可能意味着它不会做铸造他们。这意味着,OGG是特殊的,它需要铸造。任何线索?

I don't know why it's casting my FileHandle to AndroidFileHandle while loading OGG file. If it loads fine other type of files, that probably means it doesn't do casting for them. That means that OGG is special and it needs casting. Any clues?

推荐答案

我解决了这个问题解压压缩到特定文件夹, 然后读取外部文件夹。

I solved this problem extracting the zip to a specific folder, and then reading from that external folder.

拉​​链的提取是通过以下方法进行:

The extraction of the zip is done by these methods:

    public void extract(){
    String packageName = getPackageName();
    File root = Environment.getExternalStorageDirectory();
    File expPath = new File(root.toString() + "/Android/obb/" + packageName);

    if (expPath.exists()) {
        String strMainPath = null;
        try {
            strMainPath = expPath + File.separator + "main."
                    + getPackageManager().getPackageInfo(
                            getPackageName(), 0).versionCode + "."
                            + packageName + ".obb";


            Log.e("Extract File path", "===>"+strMainPath);

            File f=new File(strMainPath);
            if(f.exists()){
                Log.e("Extract From File path", "===> not exist");
            }
            else
            {
                Log.e("Extract From File path", "===> exist");
            }

            String pathToExtract = Environment.getExternalStorageDirectory()+"/"+Cons.FOLDERNAME;
            Log.e("Extract to path", "===>"+pathToExtract);
            flag = extractZip(strMainPath,pathToExtract);

            Log.e("After Extract Zip", "===>"+flag);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

    }

}


private boolean extractZip(String pathOfZip,String pathToExtract)
{
    int BUFFER_SIZE = 1024;
    int size;
    byte[] buffer = new byte[BUFFER_SIZE];


    try {
        File f = new File(pathToExtract);
        if(!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(pathOfZip), BUFFER_SIZE));
        fileNum=0;
        try {
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                String path = pathToExtract  +"/"+ ze.getName();

                if (ze.isDirectory()) {
                    File unzipFile = new File(path);
                    if(!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                }
                else {
                    updateFileNum();
                    FileOutputStream out = new FileOutputStream(path, false);
                    BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
                    try {
                        while ( (size = zin.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
                            fout.write(buffer, 0, size);
                        }

                        zin.closeEntry();
                    }catch (Exception e) {
                        Log.e("Exception", "Unzip exception 1:" + e.toString());
                    }
                    finally {
                        fout.flush();
                        fout.close();
                    }
                }
            }
        }catch (Exception e) {
            Log.e("Exception", "Unzip exception2 :" + e.toString());
        }
        finally {
            zin.close();
        }
        return true;
    }
    catch (Exception e) {
        Log.e("Exception", "Unzip exception :" + e.toString());
    }
    return false;

}

注:解压缩到.Android文件夹,otherwhise用户可直接存取权限的资产。例如,他们会看到在画廊的应用程序中的图像。

Note: Extract it to .Android folder, otherwhise users will have direct acces to the assets. For example they will see the images in the Gallery app.

 
精彩推荐
图片推荐