【问题标题】:How to read .wav file as byte array in android studio?如何在 android studio 中将 .wav 文件读取为字节数组?
【发布时间】:2021-04-27 21:13:40
【问题描述】:

我在 android studio 中使用 Java 来编写我的应用程序。我有一个音频文件的文件路径(.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());
    }

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

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

【问题讨论】:

  • @Jason 不是真的,我正在尝试将其转换为字节数组,但是,有一些与输出相关的行话值。例如,(dog.wav)[www.beaniebestbuy.com/sounds/dog5.wav]转换后,第一个值为:[B@d25663f.
  • [B@d25663f是数组的toString()[B表示是字节数组,d25663f是数组(身份)哈希码的十六进制形式。我不确定你希望 audioBytes.toString() 做什么。
  • @MarkRotteveel,第一个答案验证了我所做的事情是正确的。感谢您的补充说明。

标签: java android tensorflow


【解决方案1】:

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

当您在数组上调用toString 时,它不会为您提供整个数组的表示,它只是为您提供对象引用。见documentation here

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

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

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

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

【讨论】:

  • 谢谢,我对 .wav 文件格式不太熟悉。看到行话值吓坏了我。谢谢你的澄清。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 2019-05-30
  • 2013-07-31
相关资源
最近更新 更多