问题
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…
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);
回答1:
Check if you can disable ICE and/or STUN in Android.net.rtp. These are usually responsible for the delay at media setup.
回答2:
after some debugging I discovered that the AudioManager is introducing the delay in the setMode call:
myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION))
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.
See similar answer in: Is there any significant delay in initializing AudioTrack on Android?
Finally I found out this solution, setting:
myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
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...
Hope to be usefull for somobedy else.
来源:https://stackoverflow.com/questions/35239422/some-seconds-delay-on-starting-sending-voip-with-android-net-rtp