大疆msdk v5 喊话器 pcm转opus

        项目需求是将一段语音发送给喊话器,按照大疆官方例子编写代码,不知道为什么就用不了,于是就探索其他方式,在喊话器厂商的帮助下成功找到,虽然效果不是很理想,起码是有声音了。

        喊话器接受的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());
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值