java - Xuggler 中的 BigEndian、LittleEndian 混淆

标签 java xuggler endianness

之前我提出了一个关于将 byte[] 转换为 short[] 的问题,我遇到的一个新问题是转换/不转换来自 byte 的数据[] 到 BigEndian。
这是正在发生的事情:

  • 我正在使用 TargetDataLine 将数据读入 byte[10000]
  • AudioFormat 对象将 BigEndian 任意设置为 true
  • 这个 byte[] 需要转换成 short[] 才能使用 Xuggler 编码
  • 我不知道 AudioFormat BigEndian 应该设置为 true 还是 false。
    我已经尝试了这两种情况,并且在这两种情况下都出现了异常。
    要将 byte[] 转换为 short[],我这样做:

    fromMic.read(tempBufferByte, 0, tempBufferByte.length);
    for(int i=0;i<tempBufferShort.length;i++){
    tempBufferShort[i] = (short) tempBufferByte[i];
    }  
    

    地点:
    fromMicTargetDataLine
    tempBufferbytebyte[10000]
    tempBufferShortshort[10000]
    我得到异常:

       java.lang.RuntimeException: failed to write packet: com.xuggle.xuggler.IPacket@90098448[complete:true;dts:12;pts:12;size:72;key:true;flags:1;stream index:1;duration:1;position:-1;time base:9/125;]
    

    可能需要的杂项信息:

  • 我如何在 Xuggler 中设置流以添加音频:
  • writer.addAudioStream(0,1,fmt.getChannels(),(int)fmt.getSampleRate());
    

  • 我如何执行编码
  • writer.encodeAudio(1,tempBufferShort,timeStamp,TimeUnit.NANOSECONDS);
    

    关于 AudioFormat 的 Java 文档

    ...In addition to the encoding, the audio format includes other properties that further specify the exact arrangement of the data. These include the number of channels, sample rate, sample size, byte order, frame rate, and frame size...

    For 16-bit samples (or any other sample size larger than a byte), byte order is important; the bytes in each sample are arranged in either the "little-endian" or "big-endian" style.

    问题:

  • 我是否需要在 javax.sound.sampled.AudioFormat 对象中将 BigEndian 保持为 true

  • 是什么导致了错误?是格式吗?



  • 我想我得到了由 AudioFormat 对象预格式化的 BigEndian 数据。

    最佳答案

    如果你的数据确实是big endian,你可以像这样直接将它转换成一个(big endian)短数组:

    ByteBuffer buf = ByteBuffer.wrap(originalByteArray);
    short[] shortArray = buf.asShortBuffer().array();
    

    生成的 short 数组将直接包含所有原始 byte 数组,并且正确映射,前提是您的数据是大端。所以,一个原始数组,例如:

    // bytes
    [00], [ae], [00], [7f]
    

    将转换为:

    // shorts
    [00ae], [007f]
    

    关于java - Xuggler 中的 BigEndian、LittleEndian 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14033778/

    相关文章:

    java - 类的静态方法之间的同步

    java - 使用 xuggler 从图片创建 mp4 和 java mp3

    ubuntu - 在 ubuntu 11.10 中安装 xuggler 时收到错误消息

    c++ - 在一行中检测系统字节序?

    java - 我应该如何连接数组

    java - 即使我认为我已经退出循环,我的递归循环也会出现堆栈溢出错误

    java - Arraylist 检查越界索引会导致崩溃

    java - 如何合并一个音频和视频文件 -- Xuggler

    c# - 在 C# 中将数组中的字节从大端交换到小端的快速方法

    C - ntohl 用零替换数字