繁体   English   中英

如何在 android 工作室中将.wav 文件读取为字节数组?

[英]How to read .wav file as byte array in android studio?

我在 android 工作室中使用 Java 来编写我的应用程序。 我有一个音频文件(in.wav 格式)的文件路径,将 .wav 文件读入字节数组的最佳方法是什么? 我正在使用以下转换方法:

public void open_audio_file(Uri filePath){
        try{
            in = new BufferedInputStream(getContext().getContentResolver().openInputStream(filePath));

            int read;
            byte[] buff = new byte[1024];
            while ((read = in.read(buff)) > 0)
            {
                out.write(buff, 0, read);
            }
            out.flush();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        //Todo : Change the audio file to a float pointer
        audioBytes = out.toByteArray();
        Log.i(LOG_TAG,"The audio file is " + audioBytes.toString());
    }

但是,这种方法会产生很多我的 function 无法读取的行话值。 例如,dog.wav www.beaniebestbuy.com/sounds/dog5.wav 的output是 [B@d25663f. 数组的开头附加了行话值。

更新 1:我尝试使用 AudioSystem 库,但 Android 工作室不支持它。

您正在正确读取文件。 由于您只是在读取字节,因此文件类型实际上是无关紧要的。

当您在数组上调用toString时,它不会为您提供整个数组的表示,它只会为您提供 object 参考。 请参阅此处的文档

如果要查看完整数组,则需要使用for循环。

for(int i = 0; i < audioBytes.length; i++) {
    System.out.print(audioBytes[i] +" ");
}

或者,您可以使用Arrays.toString()

请注意,这可能是一个非常大的 output,不太容易阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM