【问题标题】:Audio file to byte array Android not getting converted correctly音频文件到字节数组 Android 未正确转换
【发布时间】:2017-08-29 12:03:18
【问题描述】:

我正在尝试将音频文件转换为字节数组,但似乎没有正确转换。我正在使用麦克风录制声音,然后使用设备上的文件路径将该文件转换为字节数组。

所需的字节数组应该像 0x12323

但它就像这个字符串 [B@14746f6

以下是将音频转换为字节数组的代码

file 是设备上文件的路径。文件类型是amr

 FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int read = 0;
            byte[] buffer = new byte[1024];
            while (read != -1) {
                read = fis.read(buffer);
                if (read != -1)
                    out.write(buffer,0,read);
            }
            out.close();

            byte[] bytes = out.toByteArray();
            Log.e("byte array" ,bytes.toString());

【问题讨论】:

  • [B@14746f6toString Object 方法返回的默认字符串类型。不是数组的值(字节)。
  • 如果我直接传递这个字节数组而不转换为字符串,那么传递的值也是一样的
  • [B@14746f6 这个正在传递,但我想像这样传递 0x14746f6
  • 我不知道您在file 中保存了什么,但我知道bytes.toString() 不会将数组中保存的单个字节作为字符串返回给您。
  • 使用它,看看输出是什么:Log.e("byte array" , Arrays.toString(bytes)); 你的代码看起来不错,但正如 EasyJoin 所说,你打印不正确

标签: android arrays


【解决方案1】:
String path= ""; // Audio File path
InputStream is= new FileInputStream(path); 
byte[] arr= readByte(is);
Log.e("byte: ",""+ Arrays.toString(arr)); 

【讨论】:

  • arr 是字节数组流,它对我有用,可能对其他人有帮助。
【解决方案2】:

我在与 api guy 交谈后解决了这个问题。我将字节数组转换为base64字符串并传递它。这解决了这个问题。

【讨论】:

    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 2016-04-27
    • 2018-08-11
    相关资源
    最近更新 更多