开始使用 Android.net.rtp 发送 Voip 时会延迟几秒钟 [英] Some seconds delay on starting sending Voip with Android.net.rtp

查看:67
本文介绍了开始使用 Android.net.rtp 发送 Voip 时会延迟几秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Android.net.rtp 库实现了一个使用 Voip 的 Android 应用程序.它只是从设备麦克风获取语音并将其发送到 Voip(到另一个 Android 或 PC 接收器).问题是在某些设备上 voip 传输在 2-3 秒后开始.我不是说传送数据包有 2-3 秒的延迟,我的意思是没有发送语音的前 2-3 秒.在那 2-3 秒后,一切正常.奇怪的是,它只发生在一些android设备上,而不是设备性能或Android版本的问题.例如,它发生在一个非常旧的设备和一个新设备上,而它不会发生在另一个非常旧的设备和另一个新设备上......我想到了一些延迟麦克风捕获的 Android 服务/功能,但我目前没有发现任何东西......

I implemented an Android app that uses Voip by Android.net.rtp library. It simply gets voice from device microphone and sends it in Voip (to another Android or to a PC receiver). The problem is that on some devices the voip trasmission start after 2–3 seconds. I don't mean that there is a delay of 2–3 seconds in delivering packets, I mean that the first 2–3 seconds of voice are not sended. After those 2–3 seconds everything works properly. The strange thing is that it happens only on some android device, and it is not a problem of device performance or Android version. For example it happens on a very old device and in a new one, while it doesn't happen in another very old device and in another new one… I thought to some Android service/functionality that delays mic capture, but I didn't find out anything at the moment…

下面是我用来发送Voip的代码,很经典的代码:

In the following, the code I use to send Voip, it is a classical code:

myAudioStream = new AudioStream(myIPAddress);
myAudioStream.setCodec(AudioCodec.PCMU);
myAudioGroup = new AudioGroup();
myAudioManager = (AudioManager) myContext.getSystemService(Context.AUDIO_SERVICE);

myAudioGroup.setMode(RtpStream.MODE_SEND_ONLY);
myAudioStream.join(null);
myAudioStream.setMode(RtpStream.MODE_SEND_ONLY);
myAudioStream.associate(ipAddress_Receiver, port_Receiver);
myAudioStream.join(myAudioGroup);
myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
myAudioManager.setSpeakerphoneOn(false);
myAudioManager.setMicrophoneMute(false);

推荐答案

经过一些调试,我发现 AudioManager 在 setMode 调用中引入了延迟:

after some debugging I discovered that the AudioManager is introducing the delay in the setMode call:

myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION))

奇怪的是它取决于设备.对于某些设备,它也可以引入 2-3 秒,而对于其他设备,则不会引入延迟.

The strange thing is that it depends from the device. With some devices it can introduce also 2-3 seconds, with other devices no delay is introduced.

见类似答案:在 Android 上初始化 AudioTrack 是否有明显延迟?

最后我找到了这个解决方案,设置:

Finally I found out this solution, setting:

myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

在我班级的构造函数中,然后只有一次.当我还没有启动语音 TX 时,我在我的应用程序启动时调用了 costructor.这样,当我要说话的时候,我就不用浪费那几秒钟了...

in the costructor of my class, then only once. I invoke the costructor on starting of my App, when I didn't start the voice TX yet. In this way, when I have to speak I don't have to loose those seconds...

希望对其他人有用.

这篇关于开始使用 Android.net.rtp 发送 Voip 时会延迟几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆