可能是重复问题:
在Java中将字节数组转换为短整型数组并再次转换回来
Xuggler中的encodeAudio()
方法具有以下参数:
使用
javax.sound.sampled
中的TargetDataLine
,我可以将数据读入一个byte[]
数组。
byte[] tempBuffer = new byte[10000];
fromMic.read(tempBuffer,0,tempBuffer.length);
但问题在于samples
参数需要short[]
类型。
public static byte[] toByteArray(short[] data) { byte[] bytes = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) { bytes[i * 2] = (byte) (data[i] >> 8); bytes[(i * 2) + 1] = (byte) data[i]; } return bytes; } public static short[] toShortArray(byte[] data) { short[] shorts = new short[data.length / 2]; for (int i = 0; i < shorts.length; i++) { shorts[i] = (short) ((data[i * 2] << 8) | (data[(i * 2) + 1] & 0xff)); } return shorts; }
- Rohit Jain