项目需求是将一段语音发送给喊话器,按照大疆官方例子编写代码,不知道为什么就用不了,于是就探索其他方式,在喊话器厂商的帮助下成功找到,虽然效果不是很理想,起码是有声音了。
喊话器接受的opus有格式要求。参考这个opus(一)_libopus-CSDN博客和这个在Android中实现OPUS编码_android opus-CSDN博客。后台接收pcm录音,转化成opus。
public static void cover() {
try {
FileInputStream fileIn = new FileInputStream(input);
OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);
encoder.setBitrate(16000);
encoder.setSignalType(OpusSignal.OPUS_SIGNAL_VOICE);
encoder.setComplexity(10);
encoder.setUseVBR(false);
int frameSizeMilliseconds = 60;
int encoderSampleRate = 16000;
FileOutputStream fileOut = new FileOutputStream(output);
int packetSamples = encoderSampleRate * frameSizeMilliseconds / 1000;
// int packetSamples = 960;
byte[] inBuf = new byte[packetSamples * 2 * 2];
byte[] data_packet = new byte[1280];
long start = System.currentTimeMillis();
// long granulePos = 0;
while (fileIn.available() >= inBuf.length) {
int bytesRead = fileIn.read(inBuf, 0, inBuf.length);
short[] pcm = BytesToShorts(inBuf, 0, inBuf.length);
encoder.encode(inBuf, 0, packetSamples, data_packet, 0, 1280);
fileOut.write(data_packet, 0, data_packet.length);
}
long end = System.currentTimeMillis();
System.out.println("Time was " + (end - start) + "ms");
fileIn.close();
fileOut.close();
System.out.println("Done!");
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (OpusException e) {
System.out.println(e.getMessage());
}
}